2017-10-19 131 views
1

的情况是这样的:我有一个UIScrollView形式嵌入,形式有用户可以在其中添加文本框的N个部分,所以如果一开始我有这样的:如何在不破坏约束的情况下以编程方式将UITextField添加到UIScrollView?

UITextField - textField1 
UIButton - Add 

Add按我想有这样的:

UITextField - textField1 
UITextField - textField2 
UIButton - Add 

虽然文本字段添加,约束条件不能满足,因此UI打破

behavior

的日志中:

[LayoutConstraints] 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. 
(
    "<_UILayoutSupportConstraint:0x1c4c800a0 _UILayoutGuide:0x106ccf4e0.height == 64 (active)>", 
    "<_UILayoutSupportConstraint:0x1c4c80050 V:|-(0)-[_UILayoutGuide:0x106ccf4e0] (active, names: '|':UIView:0x106ccdc10)>", 
    "<_UILayoutSupportConstraint:0x1c4c80ff0 _UILayoutGuide:0x106ccf6d0.height == 0 (active)>", 
    "<_UILayoutSupportConstraint:0x1c4c801e0 _UILayoutGuide:0x106ccf6d0.bottom == UIView:0x106ccdc10.bottom (active)>", 
    "<NSLayoutConstraint:0x1c4c80550 UIImageView:0x106cce6c0.height == 5 (active)>", 
    "<NSLayoutConstraint:0x1c4c80640 UILabel:0x106ccf070'Completa tu Perfil'.height == 24 (active)>", 
    "<NSLayoutConstraint:0x1c0c886b0 form-view.height == 579 (active, names: form-view:0x10ed307c0)>", 
    "<NSLayoutConstraint:0x1c0c8a1e0 V:|-(0)-[form-view] (active, names: form-view:0x10ed307c0, '|':UIScrollView:0x107a37000)>", 
    "<NSLayoutConstraint:0x1c0c8a320 V:[form-view]-(0)-[profiles-view] (active, names: profiles-view:0x106ccd430, form-view:0x10ed307c0)>", 
    "<NSLayoutConstraint:0x1c4c80af0 V:[_UILayoutGuide:0x106ccf4e0]-(10)-[UILabel:0x106ccf070'Completa tu Perfil'] (active)>", 
    "<NSLayoutConstraint:0x1c4c80cd0 V:[UILabel:0x106ccf070'Completa tu Perfil']-(8)-[UIImageView:0x106cce6c0] (active)>", 
    "<NSLayoutConstraint:0x1c4c80d20 V:[UIImageView:0x106cce6c0]-(0)-[UIScrollView:0x107a37000] (active)>", 
    "<NSLayoutConstraint:0x1c4c80e10 UIImageView:0x106ccddf0.top == profiles-view.top (active, names: profiles-view:0x106ccd430)>", 
    "<NSLayoutConstraint:0x1c4c80f00 V:[UIImageView:0x106ccddf0]-(0)-[_UILayoutGuide:0x106ccf6d0] (active)>", 
    "<NSLayoutConstraint:0x1c0c8bdb0 'UIView-Encapsulated-Layout-Height' UIView:0x106ccdc10.height == 667 (active)>" 
) 

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x1c0c886b0 form-view.height == 579 (active, names: form-view:0x10ed307c0)> 

因此,由于高度的限制被打破,自动版式安排下保证金的限制,允许观点打破了这样的元素。

话虽这么说,我加入限制的方式是这样的:

func addModelButtonAction(_ sender: Any) { // an IBAction 
    addButton.isHighlighted = false 
    let last = modelTextFields.last! 
    let textField = last.createCopy() // extension method for creating a copy of the last textField 
    modelTextFieldsView.insertSubview(textField, belowSubview: last) 
    let growth = last.frame.height + 8 // the 8 represents the top spacing between textFields 
    formViewHeight.constant += growth // increasing height of the view where the form is embeded 
    modelTextFieldsHeight.constant += growth // increasing height of the view where the textFields are embeded 
    UIView.animate(withDuration: 0.5) { 
     self.view.layoutIfNeeded() 
     textField.frame.origin.y += growth // moving new textField below the previous one 
    } 

    modelTextFields.append(textField) // array of UITextField to keep track of the textFields in screen 
} 

和视图的对齐矩形: view-rectangles

和视图的层次结构:

view-hierarchy

我能做些什么来使这项工作符合预期?我很感谢你的帮助,因为我一直在试图弄清楚这一点,但没有取得任何成功。

提前致谢!

回答

2

你有几个选项来解决这个问题的布局:

就个人而言,我可能会尝试在UIStackView嵌入这些观点。堆栈视图将为您管理垂直布局,允许您根据需要插入和删除子视图。如果使用故事板,UIStackView特别容易与Interface Builder一起使用。

或者,您可以对此布局使用UITableView;它会代表您管理垂直流,尽管您需要自己管理数据源和单元格,但是会从中抽象出大部分布局问题。

你也许可以得到这个布局通过仔细管理的不同约束优先,允许一些被打破的布局变化,而不任这些工作,但是这可能是一个非常轻松。

+0

使用'UIStackView'是一个聪明的一个。非常感谢!! –

+0

@AbrahamDurán当然可以!别忘了,您可以在UIView动画块中添加和删除堆栈视图的托管子视图,并且可以很好地为这些更改制作动画! –

相关问题