ios開發UIView在iphone5下自適應問題

來源:互聯網
上載者:User

我的UIViewController的Xib裡面的View是 3.5寸的即(320*480)的。

在設定自適應自動伸長以後,經常會在ViewDidLoad方法裡面初始化一些介面,這時候使用主self.view.frame時,發現frame和xib裡面的尺寸是一樣的,並沒有自適應伸長,結果很多subview錯位了,上網查了下,在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's view property
    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 calling loadView if
    it's defined, or by loading the view from the NIB that was specified in initWithNibName:bundle:.
    If neither exists, UIKit just loads an empty view.

  3. UIKit calls viewDidLoad once
    the view and its subviews have been fully loaded. At
    this point the view's frame will be whatever it was set to in the NIB, or in loadView.

  4. Something calls 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 like pushViewController:animated: orpresentModalViewController:animated:.

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

  6. UIKit calls viewWillAppear:. The
    frame should now be the size that will be displayed.

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

  8. UIKit calls viewDidAppear:.

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.

其實代碼是沒錯的,不管是在xib裡面設定自適應,還是在ViewDidLoad方法裡賣弄設定,得到的self.view.frame始終是和xib裡面的尺寸是一樣的,這時你就鬱悶了,因為很多subView的尺寸要根據self.view的尺寸來設定。

所以對subView的一些適應性設定,不能在Viewdidload裡面,而是需要在viewWillAppear和viewDidAppear裡面完成,在這兩個方法裡面self.view.frame是自適應之後的值。

自己認為最好的方式,還是對subview設定好合適autoresizingMasks。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.