Ios-based UIView adaptation in iPhone 5

Source: Internet
Author: User

In my uiviewcontroller's XIB, the view is 3.5 (320*480.

After auto-scaling is set, some interfaces are often initialized in the viewdidload method. At this time, the main self is used. view. frame, we found that the size of the frame is the same as that of the XIB, and there is no adaptive stretching. As a result, many subviews are misplaced. I checked it online and found a description on stackoverflow:

 

The frame is not guaranteed to be the same in viewdidload as it will be when the view is eventually displayed. uikit adjusts the frame of your view controller's view prior to displaying it, based on the context in which will appear. the size is determined based
On interface orientation and the dimensions of any visible navigation bar, Tab bar, toolbar, or status bar (which itself has a height that can change, e.g. when you're on a phone call ).

It helps to understand what happens when a View Controller's view is loaded and displayed:

  1. Something accesses your view controller'sviewProperty
    For the first time. This may occur in your own code, or in uikit in response to a user action like selecting a tab.

  2. Uikit lazy-loads your view controller's view by callingloadViewIf
    It's defined, or by loading the view from the nib that was specified ininitWithNibName:bundle:.
    If neither exists, uikit just loads an empty view.

  3. Uikit cballsviewDidLoadOnce
    The view and its subviews have been fully loaded.
    This point the view's frame will be whatever it was set to in the nib, or inloadView.

  4. Something CILS for UIKit to display your view controller's view. Again, this may be a user action like tapping on a tab, or an explicit method call in your code likepushViewController:animated:OrpresentModalViewController:animated:.

  5. UIKit resizes the view based on the context in which it will be presented, as described above.

  6. UIKit cballsviewWillAppear:.
    Frame shoshould now be the size that will be displayed.

  7. UIKit displays the view, with or without animations.

  8. UIKit cballsviewDidAppear:.

As you can see, if you need to know the size of your view's frame before it gets presented,viewWillAppear:Is
Your one and only opportunity. just remember that this size may change after the view appears for various reasons, including rotation events or changes in status bar height. for this reason, it's important to give every subview an appropriate autoresizingMask,
To ensure that the layout can adjust itself properly for any change in bounds.

If you wish to build your view hierarchy manually, the recommended place to do so is in loadView. since you construct the view yourself in this method, you can initialize its frame to whatever you 'd like. the size you choose doesn' t matter much, since UIKit
Is likely to change it on you anyway. Just make sure you set your autoresizingMasks appropriately.

In fact, the code is correct. Whether it is set adaptive in xib or in the ViewDidLoad method, the self is obtained. view. the size of the frame is always the same as that of the xib, so you are depressed, because the size of many subviews depends on self. view Size.

Therefore, some adaptive settings for subview cannot be included in viewdidload, but must be completed in viewwillappear and viewdidappear. In these two methods, self. View. frame is an adaptive value.

I think the best way is to set autoresizingmasks for subview.

Related Article

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.