[英]XIB view has larger bound values and doesn't fit the screen
I have a XIB view. 我有XIB视图。
When i added it to my view, i want the width and height to fit the iOS device. 当我将其添加到视图中时,我希望其宽度和高度适合iOS设备。
However, it doesn't fit. 但是,它不合适。
If i use autolayout it would fit the screen. 如果我使用自动布局,它将适合屏幕。 But how do i use it ? 但是我该如何使用呢?
In the code example below, it takes the Bounds
of the XIB file. 在下面的代码示例中,它采用了XIB文件的Bounds
。 Therefore it doesn't fit the view. 因此,它不适合该视图。 How can i solve this? 我该如何解决?
-(id)initWithFrame:(CGRect)frame{
self = [super initWithFrame:frame];
if (self) {
self.myView=[[[NSBundle mainBundle] loadNibNamed:@"MyView" owner:self options:nil] lastObject];
self.bounds=self.myView.bounds;
[self addSubview:self.myView];
}
return self;
}
Load your view, then 加载您的视图,然后
[_subview setTranslatesAutoresizingMaskIntoConstraints:NO];
[_contentView addSubview:_subview];
[self activateConstraintsForView:_subview respectToParentView:_contentView];
Use this function : 使用此功能:
- (void)activateConstraintsForView:(UIView *)view respectToParentView:(UIView *)parentView
{
NSLayoutConstraint *bottomConstraint = [NSLayoutConstraint constraintWithItem:view
attribute:NSLayoutAttributeBottom
relatedBy:NSLayoutRelationEqual
toItem:parentView
attribute:NSLayoutAttributeBottom
multiplier:1.0
constant:0];
NSLayoutConstraint *topConstraint = [NSLayoutConstraint constraintWithItem:view
attribute:NSLayoutAttributeTop
relatedBy:NSLayoutRelationEqual
toItem:parentView
attribute:NSLayoutAttributeTop
multiplier:1.0
constant:0];
NSLayoutConstraint *leftConstraint = [NSLayoutConstraint constraintWithItem:view
attribute:NSLayoutAttributeLeft
relatedBy:NSLayoutRelationEqual
toItem:parentView
attribute:NSLayoutAttributeLeft
multiplier:1.0
constant:0];
NSLayoutConstraint *rightConstraint = [NSLayoutConstraint constraintWithItem:view
attribute:NSLayoutAttributeRight
relatedBy:NSLayoutRelationEqual
toItem:parentView
attribute:NSLayoutAttributeRight
multiplier:1.0
constant:0];
[NSLayoutConstraint activateConstraints:[NSArray arrayWithObjects:topConstraint, leftConstraint, bottomConstraint, rightConstraint, nil]] ;
}
Hope, it helps. 希望能帮助到你。
In your initWithFrame method add this at the end: 在您的initWithFrame方法中,最后添加:
[self setConstraintsOfView:self.myView withRespectToParentView:self];
And add the method below at the end of your class: 并在课程末尾添加以下方法:
-(void)setConstraintsOfView:(UIView *)subView withRespectToParentView:(UIView *)parentView
{
NSDictionary *viewsDictionary = @{@"subView":subView};
NSDictionary *metrics =@{@"offset":@0};
NSArray *heightConstraint = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-offset-[subView]-offset-|"
options:0
metrics:metrics
views:viewsDictionary];
NSArray *widthConstraint = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-offset-[subView]-offset-|"
options:0
metrics:metrics
views:viewsDictionary];
[parentView addConstraints:heightConstraint];
[parentView addConstraints:widthConstraint];
}
VFL is much easier than setting each constraints. VFL比设置每个约束要容易得多。 This method sets the top, bottom, leading and trailing space = 0 of subview with respect to superview. 此方法设置子视图相对于超级视图的顶部,底部,前导和尾随空间= 0。
I think you want to achieve this though: https://github.com/PrajeetShrestha/ConstraintTest 我认为您想实现这一目标: https : //github.com/PrajeetShrestha/ConstraintTest
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.