简体   繁体   English

通过代码自动布局问题?

[英]autolayout problems by code?

i use autolayout without sb and xib.But i have some problems recently. 我不使用sb和xib使用autolayout。但是最近我遇到了一些问题。

i don't understand which view should i use to implement the following two methods 我不知道应该使用哪种视图来实现以下两种方法

- (void)addConstraint:(NSLayoutConstraint *)constraint

- (void)addConstraints:(NSArray<__kindof NSLayoutConstraint *> *)constraints

for example,i have a super view as following: 例如,我有一个超级视图,如下所示:

_menuView = [[UIScrollView alloc] init];
_menuView.translatesAutoresizingMaskIntoConstraints = NO;
[self.view addSubview:_menuView];
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-0-[_menuView]-0-|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(_menuView)]];
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-0-[_menuView(40)]" options:0 metrics:nil views:NSDictionaryOfVariableBindings(_menuView)]];

and two subViews:view1,view2.When i use autolayout to add constraints to describe view1 and view2, 和两个subViews:view1,view2。当我使用自动布局添加约束来描述view1和view2时,

[WHICHVIEW addConstraint:<#(nonnull NSLayoutConstraint *)#>];

or [WHICHVIEW addConstraints:<#(nonnull NSArray<__kindof NSLayoutConstraint *> *)#>]; 或[WHICHVIEW addConstraints:<#(nonnull NSArray <__ kindof NSLayoutConstraint *> *)#>];或

what should WHICHVIEW should be? WHICHVIEW应该是什么? Let me put it another way,is WHICHVIEW depend on the relationship between view1 and view2?what if view1 is not the same hierarchy as view2? 让我换一种说法,WHICHVIEW是否依赖于view1和view2之间的关系?如果view1与view2的层次结构不同怎么办?

Do not use any WHICHVIEW . 不要使用任何 WHICHVIEW Do not call addConstraints: . 不要调用addConstraints: Call NSLayoutConstraint.activateConstraints instead. 而是调用NSLayoutConstraint.activateConstraints It has the advantage that it does all that work for you - it adds the constraints to the correct views, automatically. 它的优点是可以为您完成所有工作-自动将约束添加到正确的视图中。

WHICHVIEW should be a parent of [all] the items you are trying to constrain. WHICHVIEW应该是您要限制的所有项目的父项。 So if you are adding a number of views to a scrollview and trying to constrain them, with respect to themselves and to the scrollview, the scrollview should be WHICHVIEW. 因此,如果您要向滚动视图添加许多视图,并试图限制它们相对于自身以及滚动视图的视图,则滚动视图应为WHICHVIEW。 If view1 and view2 are not at all in the same hierarchy, then you cannot add a constraint between the two. 如果view1和view2根本不在同一层次结构中,则不能在两者之间添加约束。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM