我一直在做 Apple 的 iOS 教程;开始开发 iOS 应用程序。 https://developer.apple.com/library/content/referencelibrary/GettingStarted/DevelopiOSAppsSwift/ImplementingACustomControl.html#//apple_ref/doc/uid/TP40015214-CH19-SW1
在教程中,我在水平堆栈 View 上添加了一个大小限制为 44.0x44.0 的按钮。 但是,模拟器会显示一个更大的按钮。
looks the size constraints do not work.
我的代码如下。你能给出修复的建议吗?
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)
}
}
通过更改 StackView 的属性解决了这个问题。
最佳答案
我遇到了同样的问题。这是因为我没有将新的 Horizontal StackView 拖到现有的 Vertical StackView 中。相反,我将它拖到主视图中。如果您查看 Document Outline,新的 StackView 应该是第一个 Vertical StackView 的子级,并且就在 Photo Image View 的下方。
它在说明中,但我不清楚。
关于ios - heightAnchor.constraint 不起作用(使用 Swift 的 Apple FoodTracker 教程),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46605871/