• 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:

    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:

    blue constraints

    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.

    pink constraints

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

    enter image description here

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

    containers laid out

    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:

    top aspect

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

    top equal size

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

    top less than or equal size

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

    top equal size again

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

    top equal not required

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

    top centered

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

    incorrect top layout

    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:

    fix top constraints

    Xcode updates the layout:

    correct top 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:

    correct top and bottom layout

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

    iphone 4s test

    And I can test in in the iPhone 6 simulator:

    iphone 6 test

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

  • 相关阅读:
    单例模式(Singleton)在SQLite操作中的应用
    android中自定義progress
    eclipse中高亮显示相同的变量
    android定时滚动
    Android GridView中设置了Button以后就不能响应OnItemClick()
    java中转换文件大小
    android 的代码仓库
    Eclipse中代码提示功能补全
    poj 3635 Full Tank? (优先队列 + bfs)
    hdu 4279 Number (规律题 2012 ACM/ICPC Asia Regional Tianjin Online )
  • 原文地址:https://www.cnblogs.com/wcLT/p/4733650.html
Copyright © 2020-2023  润新知