0
我正在使用autolayout
。因爲我只能添加constraints
到view
,但最大的情況下我必須使用滾動條。因此,如果我使用CGRectmake方法在scrollView
上添加view
。然後它不會在橫向模式下更改尺寸。使用自動佈局的滾動條
快照在人像模式:
快照在橫向模式下:
代碼:
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模式下工作的視圖?
正如我在編碼中所做的那樣,而且我在編程時也做了與在storyboard中做的相同的事情,除了'size classes'。 @apexa –
仍然在尋找答案,代碼有什麼問題。 –