2015-09-09 168 views
0

我正在使用autolayout。因爲我只能添加constraintsview,但最大的情況下我必須使用滾動條。因此,如果我使用CGRectmake方法在scrollView上添加view。然後它不會在橫向模式下更改尺寸。使用自動佈局的滾動條

快照在人像模式:

enter image description here

enter image description here

快照在橫向模式下:

enter image description here

enter image description here

代碼:

UIScrollView *scrllView=[UIScrollView new]; 
    [scrllView setTranslatesAutoresizingMaskIntoConstraints:NO]; 

    [self.view addSubview:scrllView]; 
    [self.view addConstraint:[NSLayoutConstraint constraintWithItem:scrllView attribute:NSLayoutAttributeTrailing relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeTrailing multiplier:1.0 constant:0]]; 

    [self.view addConstraint:[NSLayoutConstraint constraintWithItem:scrllView attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeLeading multiplier:1.0 constant:0]]; 


    [self.view addConstraint:[NSLayoutConstraint constraintWithItem:scrllView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeTop multiplier:1.0 constant:0]]; 

    [self.view addConstraint:[NSLayoutConstraint constraintWithItem:scrllView attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeBottom multiplier:1 constant:0]]; 


    [scrllView setContentSize:CGSizeMake(self.view.frame.size.width, 1000)]; 


    UIView *SBV=[UIView new]; 
    [SBV setFrame:CGRectMake(0, 0, self.view.frame.size.width,1000)]; 
    [SBV setTranslatesAutoresizingMaskIntoConstraints:YES]; 
    [SBV setBackgroundColor:[UIColor redColor]]; 
    [scrllView addSubview:SBV]; 

    UIButton *BTN=[UIButton new]; 

    [BTN setTranslatesAutoresizingMaskIntoConstraints:NO]; 

    [BTN setTitle:@"UPSide" forState:UIControlStateNormal]; 

    [BTN setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; 

    [SBV addSubview:BTN]; 


    [SBV addConstraint:[NSLayoutConstraint constraintWithItem:BTN attribute:NSLayoutAttributeTrailing relatedBy:NSLayoutRelationEqual toItem:SBV attribute:NSLayoutAttributeLeading multiplier:1.0 constant:100]]; 

    [SBV addConstraint:[NSLayoutConstraint constraintWithItem:BTN attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:SBV attribute:NSLayoutAttributeLeading multiplier:1.0 constant:0]]; 


    [SBV addConstraint:[NSLayoutConstraint constraintWithItem:BTN attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:SBV attribute:NSLayoutAttributeTop multiplier:1.0 constant:0]]; 

    [SBV addConstraint:[NSLayoutConstraint constraintWithItem:BTN attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:0 constant:100]]; 


    UIButton *BTN1=[UIButton new]; 
    [BTN1 setTranslatesAutoresizingMaskIntoConstraints:NO]; 
    [BTN1 setTitle:@"DOWNSide" forState:UIControlStateNormal]; 
    [BTN1 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; 
    [SBV addSubview:BTN1]; 


    [SBV addConstraint:[NSLayoutConstraint constraintWithItem:BTN1 attribute:NSLayoutAttributeTrailing relatedBy:NSLayoutRelationEqual toItem:SBV attribute:NSLayoutAttributeLeading multiplier:1.0 constant:100]]; 

    [SBV addConstraint:[NSLayoutConstraint constraintWithItem:BTN1 attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:SBV attribute:NSLayoutAttributeLeading multiplier:1.0 constant:0]]; 


    [SBV addConstraint:[NSLayoutConstraint constraintWithItem:BTN1 attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:SBV attribute:NSLayoutAttributeBottom multiplier:1.0 constant:0]]; 

    [SBV addConstraint:[NSLayoutConstraint constraintWithItem:BTN1 attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:0 constant:100]]; 

如何我可以創建應在景觀和porttrait模式下工作的視圖?

回答

0
  1. 禁用大小班用自動佈局的。(從視圖控制器的文件檢查器的界面生成器文件)
  2. 舉一個滾動視圖中的ViewController的View.Take一種觀點認爲它裏面。
  3. 現在選擇scrollView並從底欄[Pin]中選擇第二個選項,然後選擇所有四個約束(Leading,Top,Bottom,Trailing)並取消選中constarint Margin Button.Add 4 Constraints。
  4. 現在選擇查看並從底欄[Pin]中選擇第二個選項,然後選擇所有四個約束(Leading,Top,Bottom,Trailing)和取消選中constarint Margin Button。也選擇高度限制(如果高度大於568,則設置底部約束0)。現在添加5個約束。並從底欄[對齊]中選擇第一個選項。並在容器中選擇第二個最後選項Horizo​​natal中心並添加1個約束。
+0

正如我在編碼中所做的那樣,而且我在編程時也做了與在storyboard中做的相同的事情,除了'size classes'。 @apexa –

+0

仍然在尋找答案,代碼有什麼問題。 –