IOS之constraints

anyway, you can do this with auto layout. You can do it entirely in IB as of Xcode 5.1. Let‘s start with some views:

The light green view has an aspect ratio of 4:1. The dark green view has an aspect ratio of 1:4. I‘m going to set up constraints so that the blue view fills the top half of the screen, the pink view fills the bottom half of the screen, and each green view expands as much as possible while maintaining its aspect ratio and fitting in its container.

First, I‘ll create constraints on all four sides of the blue view. I‘ll pin it to its nearest neighbor on each edge, with a distance of 0. I make sure to turn off margins:

Note that I don‘t update the frame yet. I find it easier to leave room between the views when setting up constraints, and just set the constants to 0 (or whatever) by hand.

Next, I pin the left, bottom, and right edges of the pink view to its nearest neighbor. I don‘t need to set up a top edge constraint because its top edge is already constrained to the bottom edge of the blue view.

I also need an equal-heights constraint between the pink and blue views. This will make them each fill half the screen:

If I tell Xcode to update all the frames now, I get this:

So the constraints I‘ve set up so far are correct. I undo that and start work on the light green view.

Aspect-fitting the light green view requires five constraints:

  • A required-priority aspect ratio constraint on the light green view. You can create this constraint in a xib or storyboard with Xcode 5.1 or later.
  • A required-priority constraint limiting the width of the light green view to be less than or equal to the width of its container.
  • A high-priority constraint setting the width of the light green view to be equal to the width of its container.
  • A required-priority constraint limiting the height of the light green view to be less than or equal to the height of its container.
  • A high-priority constraint setting the height of the light green view to be equal to the height of its container.

Let‘s consider the two width constraints. The less-than-or-equal constraint, by itself, is not sufficient to determine the width of the light green view; many widths will fit the constraint. Since there‘s ambiguity, autolayout will try to choose a solution that minimizes the error in the other (high-priority but not required) constraint. Minimizing the error means making the width as close as possible to the container‘s width, while not violating the required less-than-or-equal constraint.

The same thing happens with the height constraint. And since the aspect-ratio constraint is also required, it can only maximize the size of the subview along one axis (unless the container happens to have the same aspect ratio as the subview).

So first I create the aspect ratio constraint:

Then I create equal width and height constraints with the container:

I need to edit these constraints to be less-than-or-equal constraints:

Next I need to create another set of equal width and height constraints with the container:

And I need to make these new constraints less than required priority:

Finally, you asked for the subview to be centered in its container, so I‘ll set up those constraints:

Now, to test, I‘ll select the view controller and ask Xcode to update all the frames. This is what I get:

Oops! The subview has expanded to completely fill its container. If I select it, I can see that in fact it‘s maintained its aspect ratio, but it‘s doing an aspect-fill instead of an aspect-fit.

The problem is that on a less-than-or-equal constraint, it matters which view is at each end of the constraint, and Xcode has set up the constraint opposite from my expectation. I could select each of the two constraints and reverse its first and second items. Instead, I‘ll just select the subview and change the constraints to be greater-than-or-equal:

Xcode updates the layout:

Now I do all the same things to the dark green view on the bottom. I need to make sure its aspect ratio is 1:4 (Xcode resized it in a weird way since it didn‘t have constraints). I won‘t show the steps again since they‘re the same. Here‘s the result:

Now I can run it in the iPhone 4S simulator, which has a different screen size than IB used, and test rotation:

And I can test in in the iPhone 6 simulator:

I‘ve uploaded my final storyboard to this gist for your convenience.

时间: 2024-08-09 22:01:31

IOS之constraints的相关文章

iOS Programming Auto Layout: Programmatic Constraints 自动布局:通过编程限制

iOS Programming? Auto Layout: Programmatic Constraints? 1.? However, if your views are created in code, then you will need to constrain them programmatically. 如果你的view是由代码创建的,那么你需要用编程限制它了. To have a view to work with, you are going to recreate the im

Chapter 16 Auto layout: Programmatic Constraints

1. The line of code regarding translating constraints has to do with an older system for scaling interfaces - autoresizing masks. Before Auto Layout was introduced, iOS applications used autoresizing masks to allow views to scale for different-sized

cordova for ios: Unable to simultaneously satisfy constraints.

使用cordova开发ios项目的时候,在上传图片碰到一个问题.使用html的<input type="file"/>标签来选择照片或者拍照片,引起了布局报错,然后图片上传过程中很容易崩溃闪退. html页面代码: <html><head></head> <body> <input type="file" /> </body> </html> 然后在真机测试,点击选择文件

iOS使用代码调整约束做动画效果时出现Unable to simultaneously satisfy constraints.

出现这个问题时, 首先应该仔细看错误, 去除多余的存在冲突的约束, 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;

iOS 使用LayoutGuide 来限制控件的位置,配合Auto Layout constraints

UIButton *button = [UIButton buttonWithType:UIButtonTypeDetailDisclosure]; [self.view addSubview:button]; [button setTranslatesAutoresizingMaskIntoConstraints: NO]; // 得到当前视图的最低基准限制,这个是对于Auto Layout constraint来说的. id bottomGuide = self.bottomLayoutGu

WebRTC iOS平台的基本实现

前面介绍了如何下载编译WebRTC,现在介绍如何利用WebRTC在iOS客户端上简单实现音视频通话. 对下载编译还有问题的,请先查看:WebRTC(iOS)下载编译. 不需要下载源码只需要库文件的也可以用CocoaPods下载编译好的库:pod 'libjingle_peerconnection' 开始编写之前,我们首先要搭建一个服务器,此服务器主要用于信令交互.我们这里采用github上的开源项目:SkyRTC项目. 完整Demo下载:FLWebRTCDemo. 开始iOS客户端的实现: 1.

用Xamarin和Visual Studio编写iOS App

一说开发 iOS app,你立马就会想到苹果的开发语言 Objective C/Swift 和 Xcode.但是,这并不是唯一的选择,我们完全可以使用别的语言和框架. 一种主流的替换方案是 Xamarin,这是一个跨平台框架,允许你开发 iOS.Android 和 OSX.Windows app,它使用的是 C# 和 Visual Studio.最大的好处在于,Xamarin 允许你在 iOS 和 Android app 间共享代码. Xamarin 与其他跨平台框架相比有一个最大的好处:使用

iOS Core Animation Advanced Techniques(一):图层树、寄宿图以及图层几何学

(一)图层的树状结构 巨妖有图层,洋葱也有图层,你有吗?我们都有图层 -- 史莱克 Core Animation其实是一个令人误解的命名.你可能认为它只是用来做动画的,但实际上它是从一个叫做Layer Kit这么一个不怎么和动画有关的名字演变而来,所以做动画这只是Core Animation特性的冰山一角. Core Animation是一个复合引擎,它的职责就是尽可能快地组合屏幕上不同的可视内容,这个内容是被分解成独立的图层,存储在一个叫做图层树的体系之中.于是这个树形成了UIKit以及在iO

- (void)addConstraints:(NSArray&lt;__kindof NSLayoutConstraint *&gt; *)constraints

Adds multiple constraints on the layout of the receiving view or its subviews.   All constraints must involve only views that are within scope of the receiving view. Specifically, any views involved must be either the receiving view itself, or a subv