UIViewController的edgesForExtendedLayout屬性

來源:互聯網
上載者:User

標籤:

想必大家都遇到一種情況,明明y座標設定的是0,但是總是被討厭的導覽列給遮住。比如下面這個情況:

UILabel *label = [[UILabel alloc] init];label.frame = CGRectMake(10, 0, SCREEN.width - 20, 88);label.backgroundColor = [UIColor redColor];label.text = @"關注公眾號iOS開發:iOSDevTip";label.textColor = [UIColor whiteColor];label.textAlignment = NSTextAlignmentCenter;[self.view addSubview:label];

一起來看看效果:


是不是很討厭!其實,在iOS 7中,UIViewController引入了一個新的屬性:edgesForExtendedLayout。 這個屬性的預設值是UIRectEdgeAll。當你的容器是UINavigationController的shih,預設的布局就是從狀態列的頂部開始的。這就是為什麼你設定的控制項都往上漂移了66ot的原因。

   @property(nonatomic,assign) UIRectEdge edgesForExtendedLayout NS_AVAILABLE_IOS(7_0); // Defaults to UIRectEdgeAll

那麼如何解決這個問題呢?有兩種方法。

方法一:改變edgesForExtendedLayout
self.edgesForExtendedLayout = UIRectEdgeNone;

將edgesForExtendedLayout屬性設定為UIRectEdgeNone,這樣布局就是從導覽列下面開始了。設定之後,再來看看效果:


方法二:導覽列半透明屬性設定為NO
@property(nonatomic,assign,getter=isTranslucent) BOOL translucent NS_AVAILABLE_IOS(3_0) UI_APPEARANCE_SELECTOR; // Default is NO on iOS 6 and earlier. Always YES if barStyle is set to UIBarStyleBlackTranslucent

在iOS 6之前(包括iOS 6)translucent預設就是NO,在iOS 7就預設是YES了。

self.navigationController.navigationBar.translucent = NO;

將導覽列的半透明屬性關閉掉,布局也是從導覽列下面開始了。


UIViewController的edgesForExtendedLayout屬性

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.