【问题标题】:iOS - Set proper constraint for dynamiclly created viewsiOS - 为动态创建的视图设置适当的约束
【发布时间】:2014-09-08 11:11:11
【问题描述】:

我有这样的想法(对不起,这只是为了测试丑陋的代码):

override func viewDidLoad() {
    super.viewDidLoad()
    self.emptyScrollView.scrollEnabled = true
    var emptyView = UIView()
    self.emptyScrollView.addSubview(emptyView)
    var frame2 = CGRect()
    frame2 = emptyView.frame
    frame2.origin.x = 0
    frame2.origin.y = 10
    frame2.size.width = self.view.frame.width
    emptyView.frame = frame2
    var textView = UITextView()
    textView.text = sampleText
    var frame = CGRect()
    frame = emptyView.frame
    textView.frame = frame
    textView.font = UIFont(name: "Helvetica Neue", size: 14)
    textView.textColor = UIColor.blackColor()
    textView.backgroundColor = UIColor.redColor()
    emptyView.addSubview(textView)
    textView.sizeToFit()
    textView.layoutIfNeeded()

    var bootomY = CGRectGetMaxY(textView.frame)
    var emptyView2 = UIView()
    self.emptyScrollView.addSubview(emptyView2)
    var frame3 = CGRect()
    frame3 = emptyView2.frame
    frame3.origin.x = 0
    frame3.origin.y = bootomY
    frame3.size.width = self.view.frame.width
    emptyView2.frame = frame3
    var textView2 = UITextView()
    textView2.text = sampleText2
    var frame4 = CGRect()
    frame4 = emptyView2.frame
    textView2.frame = frame4
    textView2.font = UIFont(name: "Helvetica Neue", size: 14)
    textView2.textColor = UIColor.blackColor()
    emptyView2.addSubview(textView2)
    textView2.sizeToFit()
    textView2.layoutIfNeeded()

    var scrollHeight = textView.frame.height + textView2.frame.height + 1000
    self.emptyScrollView.contentSize = CGSize(width: self.view.frame.width, height: scrollHeight)

    var constraint = NSLayoutConstraint(item: emptyView, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: emptyView2, attribute: NSLayoutAttribute.Top, multiplier: 1.0, constant: 10)
    self.emptyScrollView.addConstraint(constraint)
}

当我运行应用程序时,出现错误:

014-09-08 13:05:20.323 CutsomViewWithText[9123:3532873] Unable to simultaneously satisfy constraints.
Probably at least one of the constraints in the following list is one you don't want. Try this: (1) look at each constraint and try to figure out which you don't expect; (2) find the code that added the unwanted constraint or constraints and fix it. (Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints) 
(
"<NSLayoutConstraint:0x7ba39240 UIView:0x7a758e30.bottom == UIView:0x7a76d3e0.top + 10>",
"<NSAutoresizingMaskLayoutConstraint:0x7a74c470 h=--& v=--& UIView:0x7a758e30.midY == + 10>",
"<NSAutoresizingMaskLayoutConstraint:0x7a75c480 h=--& v=--& V:[UIView:0x7a758e30(0)]>",
"<NSAutoresizingMaskLayoutConstraint:0x7a76a990 h=--& v=--& UIView:0x7a76d3e0.midY == + 226.5>",
"<NSAutoresizingMaskLayoutConstraint:0x7a76a9c0 h=--& v=--& V:[UIView:0x7a76d3e0(0)]>"
)

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x7ba39240 UIView:0x7a758e30.bottom == UIView:0x7a76d3e0.top + 10>

Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h>   may also be helpful.
2014-09-08 13:05:23.663 CutsomViewWithText[9123:3533398] Terminating since there is no system app.

你能帮我吗,我应该怎么做才能在第一视图和第二视图之间设置适当的约束?

【问题讨论】:

    标签: ios iphone swift constraints


    【解决方案1】:

    您需要添加以下代码行:

        emptyView.setTranslatesAutoresizingMaskIntoConstraints(false)
        emptyView2.setTranslatesAutoresizingMaskIntoConstraints(false)
    

    之前

        var constraint = NSLayoutConstraint(item: emptyView, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: emptyView2, attribute: NSLayoutAttribute.Top, multiplier: 1.0, constant: 10)
    

    别忘了用合适的frame初始化emptyScrollView,否则会黑屏

    【讨论】:

      【解决方案2】:

      为视图添加的约束类型。

      左、上、宽、高

      左约束(样本):

      myView1.setTranslatesAutoresizingMaskIntoConstraints(false) myView2.setTranslatesAutoresizingMaskIntoConstraints(false)

      self.addConstraint(NSLayoutConstraint(

                      item:myView1, attribute:NSLayoutAttribute.Left,
      
                      relatedBy:NSLayoutRelation.Equal, toItem:myView2,
      
                      attribute:NSLayoutAttribute.Right, multiplier:1.0, constant: 0.0))
      

      【讨论】:

        猜你喜欢
        • 2016-10-20
        • 2021-12-23
        • 1970-01-01
        • 1970-01-01
        • 2020-02-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多