I have 8 UIViews that represent levels in my app. For each such view:
If the user has unlocked, but not completed the level, the view displays a button.
If the user has unlocked and completed the level, the view displays an image.
Otherwise, the view displays nothing.
At least, that's how it's supposed to work. In reality, there is one view that displays nothing even if I complete the level it represents. The other views work just well. However, this is the only one of the 8 views that I created in code (due to a prior problem with the view's button, which went away when I switched to using code).
Here is the view's code:
Code:
import UIKit
enum ActivityStatus {
case NotThereYet
case Next
case Completed
}
class MenuItemView: UIView {
var currentStatus : ActivityStatus = ActivityStatus.NotThereYet
var starNum : Int = 1
let button: UIButton! = UIButton()
@IBOutlet var starImageView: UIImageView? {
didSet {
/
let mainBundle = Bundle(for: MenuItemView.self)
let starURL = mainBundle.path(forResource: "\(starNum)star", ofType: "png", inDirectory:"pictures")
let starImage = UIImage(contentsOfFile: starURL!)
starImageView!.image = starImage!
button.bringSubview(toFront: button)
/
self.addSubview(self.starImageView!)
/
self.starImageView!.isHidden = true
}
}/* Star image view. Defined in storyboard **/
func buttonPressed() {
(superview as! ExerciseMenuView).buttonPressed(sender: self)
}
func addPlayButton() /
{
/
let mainBundle = Bundle(for: MenuItemView.self)
let playURL = mainBundle.path(forResource: "play-button", ofType: "png", inDirectory:"pictures")
let playImage = UIImage(contentsOfFile: playURL!)
button.setBackgroundImage(playImage, for: UIControlState.normal)
/
self.addSubview(button)
/
button.addTarget(self, action: #selector(MenuItemView.buttonPressed), for: UIControlEvents.touchUpInside)
/
button.translatesAutoresizingMaskIntoConstraints = true
/
button.frame = self.bounds
/
button.contentMode = UIViewContentMode.scaleAspectFit
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
addPlayButton()
}
override init(frame: CGRect) { /
super.init(frame: frame)
addPlayButton()
/
starImageView = UIImageView(frame: self.bounds)
}
func setActivityStatus(newStatus : ActivityStatus) {
self.isHidden = false
currentStatus = newStatus
switch newStatus {
case .Next:
button.isHidden = false
button.isUserInteractionEnabled = true
case .Completed:
starImageView!.isHidden = false
button.isHidden = true
default:
button.isHidden = true
starImageView!.isHidden = true
break
}
/
if (self.tag == 0)
{
print("View with tag \(self.tag) button \(button) imageview: \(starImageView)")
}
}
/
/
/
override func drawRect(rect: CGRect) {
/
}
*/
}A few things I've checked:
1) The image view's frame - it's what it should be
2) If the image view's set to hidden when it shouldn't be - it's not.
Also, because my UIImage is unwrapped with a !, I can rule out the possibility of a nil image.
Further, the "play" button you see in my code does show up when it is supposed to.
According to my research, if two subviews are added via addSubview, the view added last will be in front of the other. Is that correct?