ios - heightAnchor.constraint 不起作用(使用 Swift 的 Apple FoodTracker 教程)

标签 ios swift constraints

我一直在做 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 的属性解决了这个问题。

最佳答案

我遇到了同样的问题。这是因为我没有将新的 Horizo​​ntal StackView 拖到现有的 Vertical StackView 中。相反,我将它拖到主视图中。如果您查看 Document Outline,新的 StackView 应该是第一个 Vertical StackView 的子级,并且就在 Photo Image View 的下方。

它在说明中,但我不清楚。

关于ios - heightAnchor.constraint 不起作用(使用 Swift 的 Apple FoodTracker 教程),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46605871/

相关文章:

ios - Swift MKMapView.setRegion 在 MKLocalSearch.Request 之后不起作用

ios - Phonegap Barcodescanner 插件 - 适用于 iPad 和 iPhone 的定制 XIB

swift - 在 cellForItemAtIndexPath 之外访问 CollectionViewCell 属性

python - 将 UIImage 从 BGR 转换为 RGB

ios - 我如何与 AppDelegate 的特定 View 进行交互?

iphone - 是否可以在 uikit View 层次结构之上添加具有透明 BG 的 SpriteKit 场景?

ios - 如何在 Swift 中以编程方式更改 UIWebView 方向

ios - 使用 Swift 更新动画中的约束

IOS自动布局约束

gradle - 收集 Gradle 项目的依赖约束