I have a subview that is laid out in it's parent view's XIB. The subview has a button that's showing up in the view debugger but not on my device's screen. A logging statement says the button's frame is (0,0,240,128) - exactly as expected.Additionally, this is just one of many similar views, and their logging statements have the button sizes correctly set. Edit: In fact, when I break in layoutSubviews, the code debugger says the button's size is what I want it to be.
There are no Autolayout constraints.
Edit: Also, I don't manipulate the frame outside of layoutSubviews.
Edit #3:
OK, so just a clarification. Sorry for the missing details:
The "menu" is a bunch of Play buttons. These play buttons correspond to levels. When the user finishes a level, a different play button pops up. I looked into using a UICollectionView but decided it wouldn't give me the flexibility I wanted to choose where the buttons pop up.
Anyway, each button has a superview, one of which is the problem view. Now, I was able to dance around the issue by kind of swapping one view out for another, but the problem remains. I may be able to work around this by hand-coding the problem subview, but of course I really would like to know how to actually fix the problem.
Edit #2: Here is my new code:
There are no Autolayout constraints.
Edit: Also, I don't manipulate the frame outside of layoutSubviews.
Edit #3:
OK, so just a clarification. Sorry for the missing details:
The "menu" is a bunch of Play buttons. These play buttons correspond to levels. When the user finishes a level, a different play button pops up. I looked into using a UICollectionView but decided it wouldn't give me the flexibility I wanted to choose where the buttons pop up.
Anyway, each button has a superview, one of which is the problem view. Now, I was able to dance around the issue by kind of swapping one view out for another, but the problem remains. I may be able to work around this by hand-coding the problem subview, but of course I really would like to know how to actually fix the problem.
Edit #2: Here is my new code:
Code:
override func awakeFromNib() {
super.awakeFromNib()
// 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)
}
override func layoutSubviews() {
super.layoutSubviews()
// Make sure button doesn't accidentally resize
button.translatesAutoresizingMaskIntoConstraints = false
// Size button to fill view.
button.frame = self.bounds
}
Last edited: