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:
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.
Edit: Also of note, the image URL is unwrapped with a ! so it's clearly not that.
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:
class MenuItemView: UIView {
var currentStatus : ActivityStatus = ActivityStatus.NotThereYet
var starNum : Int = 1
let button: UIButton! = UIButton()
@IBOutlet var starImageView: UIImageView? {
didSet {
// Load star.
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)
// Make sure star image view is added
self.addSubview(self.starImageView!)
// Hide view
self.starImageView?.isHidden = true
}
}/** Star image view. Defined in storyboard **/
func buttonPressed() {
(superview as! ExerciseMenuView).buttonPressed(sender: self)
}
func addPlayButton() // Adds the "Play" button
{
// Give the button its image.
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)
// Add button to self.
self.addSubview(button)
// Respond to button taps
button.addTarget(self, action: #selector(MenuItemView.buttonPressed), for: UIControlEvents.touchUpInside)
// Make sure button doesn't accidentally resize
button.translatesAutoresizingMaskIntoConstraints = true
// Size button to fill view.
button.frame = self.bounds
// Size button image to fit
button.contentMode = UIViewContentMode.scaleAspectFit
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
addPlayButton()
}
override init(frame: CGRect) { // Create the missing view in level 3
super.init(frame: frame)
addPlayButton()
// Initialize the star image view.
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
}
// Log
if (self.tag == 0)
{
print("View with tag \(self.tag) button \(button) imageview: \(starImageView)")
}
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
override func drawRect(rect: CGRect) {
// Drawing code
}
*/
}
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.
Edit: Also of note, the image URL is unwrapped with a ! so it's clearly not that.
Last edited: