[英]How to resize height & width of the view using constraints for different screen?
I am very new to xocde 6 & autolayout.I am designing screens for multilple devices.I have some views such as images,textfields.I have desgined them for iPhone.I have added some fix height & width constraints.So height & width remian fix for all devices.I want to change the height & width of the view acrding to screen size.I have tried one method which was 我是xocde 6和autolayout的新手。我正在设计用于多种设备的屏幕。我有一些视图,例如图像,文本字段。我已经将它们设计用于iPhone。我添加了一些固定的高度和宽度约束。修复所有设备。我要根据屏幕尺寸更改视图的高度和宽度。我尝试了一种方法
This way view was aligned but it height was fixed.I have also used leading space,trailing space but in this way sometimes unexpected results are achieved.So is there any better way of doing it? 这种方式视图是对齐的,但高度是固定的。我也使用过引导空间,尾部空间,但有时这样会产生意想不到的结果。那么还有更好的方法吗? I am really having hard time figuring it out. 我真的很难解决这个问题。
If you're new, just for alternative I suggest you to use PureLayout. 如果您是新手,我建议您使用PureLayout作为替代。 You can use in your project with cocoapods or Carthage. 您可以将cocoapods或Carthage用于项目中。 With this library you can super easily make this constraints for different screens. 使用此库,您可以非常轻松地针对不同的屏幕设置此约束。 There is a example project in github project where you can find many different scenarios in. Here is the github page of project : github项目中有一个示例项目,您可以在其中找到许多不同的场景。这是project的github页面:
https://github.com/smileyborg/PureLayout https://github.com/smileyborg/PureLayout
Here is some example code snippet for you @deepak kumar : 这是@deepak kumar的一些示例代码片段:
- (void)updateViewConstraints
{
if (!self.didSetupConstraints) {
// Apply a fixed height of 50 pt to two views at once, and a fixed height of 70 pt to another two views
[@[self.redView, self.yellowView] autoSetViewsDimension:ALDimensionHeight toSize:50.0];
[@[self.blueView, self.greenView] autoSetViewsDimension:ALDimensionHeight toSize:70.0];
NSArray *views = @[self.redView, self.blueView, self.yellowView, self.greenView];
// Match the widths of all the views
[views autoMatchViewsDimension:ALDimensionWidth];
// Pin the red view 20 pt from the top layout guide of the view controller
[self.redView autoPinToTopLayoutGuideOfViewController:self withInset:20.0];
// Loop over the views, attaching the left edge to the previous view's right edge,
// and the top edge to the previous view's bottom edge
[[views firstObject] autoPinEdgeToSuperviewEdge:ALEdgeLeft];
UIView *previousView = nil;
for (UIView *view in views) {
if (previousView) {
[view autoPinEdge:ALEdgeLeft toEdge:ALEdgeRight ofView:previousView];
[view autoPinEdge:ALEdgeTop toEdge:ALEdgeBottom ofView:previousView];
}
previousView = view;
}
[[views lastObject] autoPinEdgeToSuperviewEdge:ALEdgeRight];
self.didSetupConstraints = YES;
}
[super updateViewConstraints];
}
I believe if you could start now, it'll be easier for you to pass auto layout headache. 我相信,如果您现在就可以开始,这将使您轻松克服自动布局难题。
I'm not sure what you're trying to ask, but it sounds like an Auto Layout question, if you don't want your Text View to fill up the entire screen, go ahead and select the Text View in your Main.storyboard and click the Pin button, like so: 我不确定您要问的是什么,但这听起来像是“ 自动布局”问题,如果您不希望“ 文本视图”填满整个屏幕,请继续在Main.storyboard中 选择 “ 文本视图”然后单击“ 固定”按钮,如下所示:
Notice that all of the constraints are enabled, which means this text view will pin to the edges of your iOS screen. 请注意,所有约束都已启用,这意味着此文本视图将固定在iOS屏幕的边缘。
If you want to later edit those constrains, go ahead and go to the Size Inspector , and you can directly click on the constrains: 如果以后要编辑这些约束,请继续并转到Size Inspector ,您可以直接单击约束:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.