Ios.autolayout.2.customviewwithautolayout

Source: Internet
Author: User

Custom View which support AutoLayout

Create a custom View that supports AutoLayout

AutoLayout reduces the burden on controller classes by making view more self-organizing.

When implementing the Custom view class, you need to provide enough information to enable the AutoLayout system to correctly compute and satisfy constraints (Constraints).

1. Specify intrinsic size for custom view (intrinsic/intrinsic size)

"Leaf-level views, such as buttons, typically know more on what size they should is than does

The code is positioning them. This is communicated through the method intrinsicContentSize ,

Which tells the layout system, there is some content it doesn ' t natively understand in a view, and

which provides to the layout system the intrinsic a size of that content. " REF[1]

"A view can implement intrinsicContentSize to return absolute values in its width and height,

Or to return NSViewNoInstrinsicMetric for either or both to indicate, it has no intrinsic metric

For a given dimension. " REF[1]

"Remember that you can also set constant width or height constraints on any view, and you don ' t

Need to override Instrinsiccontentsize if these dimensions won ' t is changing with changing view content. "

The instrinsiccontentsize default implementation of UIView is to return (Uiviewnointrinsicmetric, uiviewnointrinsicmetric).

2. Notify AutoLayout about changes in intrinsic size

Notifies the AutoLayout when the inner size of the custom view changes, notifying AutoLayout by means of invalidateintrinsiccontentsize .

3. AutoLayout operation UIView "Alignement rects" rather than frame

Constraints don't actually relate the frames of the views, rather they relate the ' alignment rects ' of views.

1 /*Set the Nsuserdefault uiviewshowalignmentrects to YES to see alignment rects drawn.2  */3-(CGRect) Alignmentrectforframe: (CGRect) frame Ns_available_ios (6_0);4-(CGRect) Frameforalignmentrect: (cgrect) Alignmentrect Ns_available_ios (6_0);5 6 /*Override this if the alignment rect are obtained from the frame by insetting each edge by a fixed amount.
This is a called by Alignmentrectforframe:and Frameforalignmentrect:.
7 */8-(Uiedgeinsets) alignmentrectinsets Ns_available_ios (6_0);

The "Alignment rects" is displayed at development time:

"

    • In the Xcode menu Product--scheme---Edit scheme (or?-<)
    • In Debug, the Arguments tab, Arguments passed on launch, click the Plus button and enter this:

      -UIViewShowAlignmentRects YES(For IOS projects)
      Or
      -NSViewShowAlignmentRects YES(for Mac OS projects)

    • You'll need the dash at the start
    • Tap OK

You should has a bunch of yellow outlines when you run "ref[3"

4. Demo for Mac OS X App

REF[2]

Reference

1. Implementing a Custom View to work with Auto Layout

2. Custom views with Auto Layout

Http://www.electricpeelsoftware.com/2013/05/08/custom-views-with-auto-layout.html

3. How to set nsviewshowalignmentrects?

Http://stackoverflow.com/questions/15394033/how-to-set-nsviewshowalignmentrects

Ios.autolayout.2.customviewwithautolayout

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.