I've been doing a iOS tutorial from Apple; Start Developing iOS Apps. https://developer.apple.com/library/content/referencelibrary/GettingStarted/DevelopiOSAppsSwift/ImplementingACustomControl.html#//apple_ref/doc/uid/TP40015214-CH19-SW1
In the tutorial, I added a button which has the size constraint of 44.0x44.0 on a Horizontal stack view. However, the simulator shows a larger button.
looks the size constraints do not work.
My code is below. Can you give advices to fix?
class RatingControl: UIStackView {
//MARK: Initialization
override init(frame: CGRect) {
super.init(frame: frame)
setupButtons()
}
required init(coder: NSCoder) {
super.init(coder: coder)
setupButtons()
}
//MARK: Private Methods
private func setupButtons() {
let button = UIButton()
button.backgroundColor = UIColor.red
// Add constraints
button.translatesAutoresizingMaskIntoConstraints = false
button.heightAnchor.constraint(equalToConstant: 44.0).isActive = true
button.widthAnchor.constraint(equalToConstant: 44.0).isActive = true
addArrangedSubview(button)
}
}
The problem was fixed by changing the property of the StackView.
I had the same issue. It was because I didn't drag the new Horizontal StackView into the existing Vertical StackView. Instead, I dragged it into the main View. If you look at the Document Outline, the the new StackView should be a child of the first Vertical StackView, and just under the Photo Image View.
It's in the instructions, but it was unclear for me.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With