2013-05-08 157 views
7

我想在代碼中生成一個視圖。這是我的看法對象UIScrollView與iOS自動佈局約束:錯誤的子視圖的大小

  • 的UIScrollView
    • 的UIView的層次結構
      • 的UIButton

滾動視圖應該是大小的窗口相同。 按鈕應儘可能大。 我使用的是iOS自動佈局,所以我所有的對象的約束字符串看起來像這樣

H:|[object]| 
V:|[object]| 

我還設置translatesAutoresizingMaskIntoConstraintsNO爲每個對象。

問題是該按鈕僅獲取默認的按鈕大小。它的父視圖對象(UIView)只能獲得其子視圖所需的大小。

enter image description here

紅:UIScrollView中/黃色:UIView的

我如何可以強制這些觀點是一樣大的滾動視圖?

當我使用的,而不是個UIScrollView的一切都很正常一個UIView ...

下面是一些代碼:

- (void) viewDidLoad { 

     [super viewDidLoad]; 

     // SCROLL VIEW 
     UIScrollView* scrollView = [UIScrollView new]; 
     scrollView.backgroundColor=[UIColor redColor]; 
     scrollView.translatesAutoresizingMaskIntoConstraints = NO; 

     //CONTAINER VIEW 
     UIView *containerView = [UIView new]; 
     containerView.translatesAutoresizingMaskIntoConstraints = NO; 
     containerView.backgroundColor = [UIColor yellowColor]; 
     [scrollView addSubview:containerView]; 

     // CONSTRAINTS SCROLL VIEW - CONTAINER VIEW 
     [scrollView addConstraints: 
     [NSLayoutConstraint constraintsWithVisualFormat:@"H:|[containerView]|" 
               options:0 metrics:nil 
                views:@{@"containerView":containerView}]]; 
     [scrollView addConstraints: 
     [NSLayoutConstraint constraintsWithVisualFormat:@"V:|[containerView]|" 
               options:0 metrics:nil 
                views:@{@"containerView":containerView}]]; 

     // BUTTON 
     UIButton* button = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
     button.translatesAutoresizingMaskIntoConstraints = NO; 
     [button setTitle:@"I'm way to small" forState:UIControlStateNormal]; 
     [containerView addSubview:button]; 

     // CONSTRAINTS CONTAINER VIEW - BUTTON 
     [containerView addConstraints: 
     [NSLayoutConstraint constraintsWithVisualFormat:@"H:|[button]|" 
               options:0 metrics:nil 
                views:@{@"button":button}]]; 
     [containerView addConstraints: 
     [NSLayoutConstraint constraintsWithVisualFormat:@"V:|[button]|" 
               options:0 metrics:nil 
                views:@{@"button":button}]]; 
     self.view = scrollView; 

    } 

UPDATE: 我真的不知道,爲什麼會這樣。如果您在IB中設置視圖,請連接插座並在代碼中實例化視圖,滾動視圖的行爲與普通視圖(垂直彈出)類似。它的contentSize計算不正確。更多here。但如何正確地做到這一點?

+0

這是一個非常複雜的解決方案,只是爲了顯示全屏按鈕。希望我回答了技術問題,但我還不清楚爲什麼你這樣做。 – Rob 2013-05-09 22:34:01

+0

請停止在帖子中寫下你的感謝 - 簽名不屬於此處。謝謝! – Undo 2013-06-02 00:42:27

回答

23

一對夫婦的意見:

  1. 約束在滾動視圖的子視圖不工作像其他視圖的約束。它們用於設置滾動視圖的contentSize。 (請參閱TN2154。)這樣,您可以在滾動視圖上投擲一堆東西,爲其中的內容設置約束條件,併爲您計算contentSize。這是非常酷的功能,但它與你在這裏嘗試做什麼是對立的。

  2. 更糟糕的是,除非您爲按鈕的寬度和高度設置明確的約束,否則按鈕會根據其內容調整大小。

這兩種意見的淨效應是,現有的限制說:「(一)設置我的容器是我的按鈕的規模;(b)讓我的按鈕,動態調整自己的文字大小;(c)根據我的容器的大小(這是按鈕的大小)設置我的滾動視圖的contentSize。「

我不清楚什麼是業務問題。但這裏有一些約束條件達到什麼樣的,我認爲你的技術問題是:

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    UIView *view = self.view; 

    UIScrollView *scrollView = [[UIScrollView alloc] init]; 
    scrollView.backgroundColor = [UIColor redColor]; // just so I can see it 
    scrollView.translatesAutoresizingMaskIntoConstraints = NO; 
    [self.view addSubview:scrollView]; 

    UIView *containerView = [[UIView alloc] init]; 
    containerView.backgroundColor = [UIColor yellowColor]; // just so I can see it 
    containerView.translatesAutoresizingMaskIntoConstraints = NO; 
    [scrollView addSubview:containerView]; 

    UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
    button.translatesAutoresizingMaskIntoConstraints = NO; 
    [button setTitle:@"I'm the right size" forState:UIControlStateNormal]; 
    [containerView addSubview:button]; 

    NSDictionary *views = NSDictionaryOfVariableBindings(scrollView, button, view, containerView); 

    // set the scrollview to be the size of the root view 

    [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[scrollView]|" 
                     options:0 
                     metrics:nil 
                     views:views]]; 

    [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[scrollView]|" 
                     options:0 
                     metrics:nil 
                     views:views]]; 

    // set the container to the size of the main view, and simultaneously 
    // set the scrollview's contentSize to match the size of the container 

    [view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[containerView(==view)]|" 
                     options:0 
                     metrics:nil 
                     views:views]]; 

    [view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[containerView(==view)]|" 
                     options:0 
                     metrics:nil 
                     views:views]]; 

    // set the button size to be the size of the container view 

    [containerView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[button(==containerView)]" 
                      options:0 
                      metrics:nil 
                      views:views]]; 

    [containerView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[button(==containerView)]" 
                      options:0 
                      metrics:nil 
                      views:views]]; 

} 

坦率地說,我不明白你的UI的商業意圖,因爲這感覺就像汽車佈局的扭曲達到了非常簡單UI。我不知道爲什麼你有一個滾動視圖,如果你有「屏幕大小」的內容(除非你是通過按鈕分頁)。我不知道爲什麼你會有一個內容視圖與一個單一的項目。我不明白你爲什麼使用全屏按鈕(我只是在當時的根視圖上放一個輕擊手勢,並稱它爲一天)。

我假設你對這一切都有很好的理由,但它可能是有意義的備份,詢問你想要的用戶體驗是什麼,然後用新的方法處理這個問題,看看是否有更有效的方法來達到預期的效果。

+0

「我不明白您的用戶界面的業務意圖......」我試圖根據其描述自動生成任何用戶界面。示例中的按鈕可以是任何一組子視圖及其子視圖。在某些情況下,描述表明,子視圖必須與超視圖一樣寬(沒有展開寬度)。 @Rob非常感謝你!!!!!! – Chrizzor 2013-05-10 08:56:15

+0

在我的示例中,他的內容大小的高度已經被正確計算,所以垂直滾動正在工作。我正在尋找像這樣的contentSize:'scrollView.contentSize = CGSizeMake(scrollView.frame.size.width,?)'。爲此,請從@Rob示例中刪除所有垂直約束:滾動仍然有效,所有子視圖可以與滾動視圖一樣寬,而不會給它們明確的寬度。 – Chrizzor 2013-05-10 09:13:14

相關問題