如何自动布局与现有的自定义视图,在iOS 6中的多个子视图 [英] How to Auto Layout with existing Custom View with multiple subviews in iOS 6
问题描述
我试图让使用约束布局,但我有内部嵌套子视图许多自定义控件。而我加入约束顶视图(CustomView),但其不停工郊游子视图正确。
I am trying to make the Layout using constraints, but I have many custom controls with internal nested Subviews. And I am adding the constraints to the top view (CustomView) but its not lay-outing the subviews properly.
防爆。我有TextFieldWithLabel类,它显示了顶部和下面一个的UITextField我创造TextFieldWithLabel的实例,并添加到超级视图与约束的标签。
Ex. I have TextFieldWithLabel class which shows the Label on top and below that a UITextField I am creating the instance of TextFieldWithLabel and adding to super view with constraints.
但它没有显示预期的结果。
虽然它的可见的,但不能放在哪里我wanted.For这个我不想改变整个TextFieldWithLabel类敌人自动布局。
But its not showing the results as expected. though its visible but not placed where I wanted.For this I dont want to change the whole TextFieldWithLabel class foe Auto Layout.
请帮助!
usernameTextField = [[TextFieldWithLabel alloc] initWithFrame:CGRectMake(0, 0, 100, 50) name:@"UserName"];
[usernameTextField setTranslatesAutoresizingMaskIntoConstraints:NO];
[superview addSubview:usernameTextField];
NSLayoutConstraint* myConstraint = [NSLayoutConstraint constraintWithItem:usernameTextField
attribute:NSLayoutAttributeCenterY
relatedBy:NSLayoutRelationEqual
toItem:superview
attribute:NSLayoutAttributeCenterY
multiplier:1
constant:0];
[superview addConstraint:myConstraint];
myConstraint = [NSLayoutConstraint constraintWithItem:usernameTextField
attribute:NSLayoutAttributeCenterX
relatedBy:NSLayoutRelationEqual
toItem:superview
attribute:NSLayoutAttributeCenterX
multiplier:1
constant:0];
[superview addConstraint:myConstraint];
截屏:
在这里,它不集中,也是文本字段(RedColor)也没有clickable.the标签和文本字段都放在里面TextFieldWithLabel。
请注意:TextFieldWithLabel是的ComponentView和ComponentView软件的子类是UIView.So的子类,我怀疑这可能是问题?我需要自动布局ComponentsView为好。
Screen shot : Here its not centered and also the text field (RedColor) is also not clickable.the label and the text field are placed inside TextFieldWithLabel. Please Note : the TextFieldWithLabel is subclass of ComponentView and ComponentView is subclass of UIView.So I suspect this may be the issue ? do I need to auto layout the ComponentsView as well.
推荐答案
在自动布局的帧大小将被忽略(你用一个中 initWithFrame:方法
)。您指定的定位限制,但没有与大小事,所以你的 TextFieldWithLabel
被定位在屏幕中央,大小为零。
Under autolayout your frame sizes will be ignored (the one you use during initWithFrame:
). You've specified positioning constraints, but nothing to do with size, therefore your TextFieldWithLabel
is positioned at the centre of the screen, with size zero.
文本字段和标签仍然是可见的,因为你说你不使用自动布局内部在此控制 - presumably你设置一些框架和口罩自动尺寸里面。因此,文本字段和标签的 TextFieldWithLabel
的范围之外,因此它们是可见的,但可能不以触摸作出反应。
The text field and label are still visible because you say you are not using autolayout internally in this control - presumably you do set some frames and autoresizing masks inside there. The text field and label are therefore outside the bounds of the TextFieldWithLabel
, so they are visible, but probably don't respond to touches.
要解决这个问题,你有两种选择:
To solve this problem you have two options:
- 使用自动布局内部 - 例如,如果你有这样的约束(在VFL),那么它会自动给一个高度,你的控制:
V:| - [标签] - [文本框] - |
-
您code尺寸添加上述限制 - 使用您在initWithFrame做了相同的尺寸。这是一个用于设置高度:
- Use autolayout internally - for example, if you have this constraint (in VFL) then it will automatically give a height to your control:
"V:|-[label]-[textField]-|"
Add sizing constraints to your code above - use the same dimensions you did in your initWithFrame. Here is one for setting the height:
[NSLayoutConstraint constraintWithItem:usernameTextField
attribute:NSLayoutAttributeHeight
relatedBy:NSLayoutRelationEqual
toItem:nil
attribute:NSLayoutAttributeNotAnAttribute
multiplier:0.0
constant:50.0];
我有一个UIView的类来这里简化的共同制约的创建。使用类code你会做 centerInView:
和 constrainToSize:
I have a category on UIView to simplify the creation of common constraints here. Using the category code you'd do centerInView:
and constrainToSize:
这篇关于如何自动布局与现有的自定义视图,在iOS 6中的多个子视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!