简体   繁体   English

如何将两个 UI 元素约束到同一个地方 - Swift iOS

[英]How to Constraint Two UI Elements to same place - Swift iOS

I'm attempting to constrain two ui elements to the same place.我试图将两个 ui 元素限制在同一个地方。 At the beginning of my program, I'm constraining a label to the center of a UiView.在我的程序开始时,我将 label 约束到 UiView 的中心。 This is working fine.这工作正常。 However later in my program, I'm attempting to remove this label from the UiView and constrain a button to the center of the same UiView.但是在我的程序后面,我试图从 UiView 中删除这个 label 并将一个按钮约束到同一个 UiView 的中心。 However, when I begin constraining my button, the system errors out.但是,当我开始约束我的按钮时,系统会出错。

How do you constrain two ui elements to the same place in Swift?如何将两个 ui 元素约束到 Swift 中的同一位置?

Here's the relevant code.这是相关的代码。

override func viewDidLoad() {
    super.viewDidLoad()
     // Middle UI View
    view.addSubview(middleUIView)
    
    NSLayoutConstraint.activate([
        middleUIView.trailingAnchor.constraint(equalTo: safeArea.trailingAnchor),
        middleUIView.leadingAnchor.constraint(equalTo: safeArea.leadingAnchor),
        middleUIView.topAnchor.constraint(equalTo: sosButton.bottomAnchor),
        middleUIView.bottomAnchor.constraint(equalTo: textView.topAnchor)
    ])
    
    // Middle Label
    middleUIView.addSubview(middleUILabel)
    
    NSLayoutConstraint.activate([
        middleUILabel.centerXAnchor.constraint(equalTo: middleUIView.centerXAnchor),
        middleUILabel.centerYAnchor.constraint(equalTo: middleUIView.centerYAnchor)
    ])
}
func createButton() {
    middleUILabel.removeFromSuperview()
        middleUIView.removeConstraints(middleUIView.constraints)
        

        // It errors out here 
        NSLayoutConstraint.activate([
            continueButton.widthAnchor.constraint(equalToConstant: 150),
            continueButton.heightAnchor.constraint(equalToConstant: 50),
            
            continueButton.centerXAnchor.constraint(equalTo: middleUIView.centerXAnchor),
            continueButton.centerYAnchor.constraint(equalTo: middleUIView.centerYAnchor)
        ])
}

This is the error:这是错误:

Terminating app due to uncaught exception 'NSGenericException', reason: 'Unable to activate constraint with anchors <NSLayoutXAxisAnchor:0x2807ffd80 "UIButton:0x135425fc0'Continue'.centerX"> and <NSLayoutXAxisAnchor:0x2807d4b00 "UIView:0x135425700.centerX"> because they have no common ancestor.由于未捕获的异常“NSGenericException”而终止应用程序,原因:“无法使用锚点激活约束 <NSLayoutXAxisAnchor:0x2807ffd80 "UIButton:0x135425fc0'Continue'.centerX"> 和 <NSLayoutXAxisAnchor:0x2807d4b00 "UIView:0x135425700.centerX"> 因为它们有没有共同的祖先。 Does the constraint or its anchors reference items in different view hierarchies?约束或其锚点是否引用不同视图层次结构中的项目? That's illegal.'这是非法的。

You need to add your button to the view hierarchy before adding contraints to it.在向其添加约束之前,您需要将按钮添加到视图层次结构中。

Also, you should keep track of the constraints you created for the label so that you can call NSContraint.deactivate() on them before you remove the label.此外,您应该跟踪为 label 创建的约束,以便在删除 label 之前对它们调用NSContraint.deactivate()

var labelConstraints: [NSLayoutConstraint] = []

override func viewDidLoad() {
    super.viewDidLoad()
     // Middle UI View
    view.addSubview(middleUIView)
    
    NSLayoutConstraint.activate([
        middleUIView.trailingAnchor.constraint(equalTo: safeArea.trailingAnchor),
        middleUIView.leadingAnchor.constraint(equalTo: safeArea.leadingAnchor),
        middleUIView.topAnchor.constraint(equalTo: sosButton.bottomAnchor),
        middleUIView.bottomAnchor.constraint(equalTo: textView.topAnchor)
    ])
    
    // Middle Label
    middleUIView.addSubview(middleUILabel)
       
    labelConstraints = [
        middleUILabel.centerXAnchor.constraint(equalTo: middleUIView.centerXAnchor),
        middleUILabel.centerYAnchor.constraint(equalTo: middleUIView.centerYAnchor)
    ]
    NSLayoutConstraint.activate(labelConstraints)
}

func createButton() {
    NSLayoutConstraint.deactivate(labelConstraints)
    labelConstraints = []
    middleUILabel.removeFromSuperview()
        
    // Add continue Button to the view hierarchy
    middleUIView.addSubview(continueButton)

    NSLayoutConstraint.activate([
        continueButton.widthAnchor.constraint(equalToConstant: 150),
        continueButton.heightAnchor.constraint(equalToConstant: 50),
            
        continueButton.centerXAnchor.constraint(equalTo: middleUIView.centerXAnchor),
        continueButton.centerYAnchor.constraint(equalTo: middleUIView.centerYAnchor)
    ])
}

Note: Manually removing constraints from views (like middleUIView.removeConstraints(middleUIView.constraints) ) is not recommended because you might remove constraints you didn't know were there.注意:不建议手动从视图中删除约束(例如middleUIView.removeConstraints(middleUIView.constraints) ),因为您可能会删除不知道的约束。 In general, you should just activate() and deactivate() constraints and let the system add them to and remove them from the proper views for you.一般来说,您应该只activate()deactivate()约束,让系统为您添加和删除它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在同一视图控制器(iOS)中将具有相同元素的不同UI应用于? - How to apply different UI with same elements in same view controller (iOS)? 你如何在 Swift/iOS 的 UI 测试中迭代元素? - how do you iterate through elements in UI testing in Swift/iOS? 如何在iOS,Swift中更改底部布局约束 - How to change bottom layout constraint in iOS, Swift Swift Autolayout:如何设置相同的距离约束 - Swift Autolayout: How to set same distance constraint 如何分组ui元素ios - how to group ui elements ios iOS快速如何将图像视图准确地放置在uiview中 - iOS swift how to place an imageview in a uiview exactly ios - 如何在ios swift中使用自动布局以编程方式将两个标签并排居中放置? - How to programmatically place two labels in center side by side with spacing using auto layout in ios swift? 如何在TableView的每一行中添加按钮,图像和其他UI元素? (iOS,快速) - How to add buttons, images and other UI elements in each row of TableView? (iOS,Swift) 两个viewController swift iOS中的uiwebview的相同实例 - same instance of uiwebview in two viewController swift ios 防止两个标记位于同一位置Google Maps SDK iOS - Preventing two markers in the same place Google Maps SDK iOS
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM