讓系統自動控制控制項在控制器視圖的位置

來源:互聯網
上載者:User

標籤:style   blog   color   使用   width   for   

IPhone、IPad經常會遇到橫豎屏切換,或者需要自動調整大小。如果你的介面不能用storyboard和xib來產生介面的話,先把控制器視圖的frame屬性值固定下來,然後添加subview(子視圖)的時候,就可以使用視圖繼承類(UIView) 內建的 autoresizingMask 屬性,之後如果橫豎屏切換,或者是使用UIPopoverController之類的方法,就可以只設定一次frame屬性,以後的frame屬性都是自適應的(frame縮小太多的話效果不好,根據情況而定)。

 原理:設定autoresizingMask後,當頁面的大小發生改變,那麼系統會給已經顯示的,所有有關的子視圖進行自動調整。屬性中的所有控制項根據 autoresizingMask 來自動化佈建屬性 frame,你能在對應的 -(void)setFrame:(CGRect)rect{} 實現系統的回調,在調用 setFrame 方法的過程中,系統會自動載入預設的動畫方法。
UIViewAutoresizing 的屬性定義如下:

 {

   UIViewAutoresizingNone              = 0,

   UIViewAutoresizingFlexibleLeftMargin  = 1 << 0,

  UIViewAutoresizingFlexibleWidth      = 1 << 1,

  UIViewAutoresizingFlexibleRightMargin = 1 << 2,

  UIViewAutoresizingFlexibleTopMargin   = 1 << 3,

   UIViewAutoresizingFlexibleHeight     = 1 << 4,

   UIViewAutoresizingFlexibleBottomMargin= 1 << 5

};

typedef NSUInteger UIViewAutoresizing;

 

UIViewAutoresizingFlexibleLeftMargin 視圖靠右對齊

UIViewAutoresizingFlexibleWidth 視圖自適應寬度

UIViewAutoresizingFlexibleRightMargin 視圖靠左對齊

UIViewAutoresizingFlexibleTopMargin 視圖靠下對齊

UIViewAutoresizingFlexibleHeight 視圖自適應高度

UIViewAutoresizingFlexibleBottomMargin 視圖靠上對齊

 

注意:LeftMargin、RightMargin、TopMargin、BottomMargin的實際對齊方向是相反的

 

樣本:讓按鈕始終在 ViewController的右上方顯示:

 

- (void)viewDidLoad{        [super viewDidLoad];    UIButton *right = [UIButton buttonWithType:UIButtonTypeRoundedRect];    right.frame = CGRectMake(self.view.frame.size.width-300, 0, 300, 300);    right.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;    [right setTitle:@"456" forState:UIControlStateNormal];    [self.view addSubview:right];}

 

聯繫我們

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