2016-03-16 1 views
5

Мне нужно спроектировать представление, как показано ниже, и я попытался с фиксированными высотами, а также попытался компактную ширину и обычную высоту, правильную ширину и компактную высоту, эти сценарии не сработали для меня.Как установить Высота высоты в процентах от высоты экрана в раскопках с XCode 7

Как установить высоту высоты в процентах от высоты экрана в раскадровки?

Я использую Xcode 7

enter image description here

ответ

6

В основном вы должны действовать на multiplier собственности на высоте равной ограничения. Чтобы сделать это, нажимая CTRL, перетащите его из представления в его супервизор и выберите equal heights constraint, затем отредактируйте это ограничение в инспекторе размеров, установив его multiplier. Желаемое значение может быть выражено также как 1:25, 1/25, 0,025. Если он работает наоборот, просто переверните элементы, как на картинке.
enter image description here

0

Programmatic версия

- (void)overallViewConstratints { 

_firstView = [[UIView alloc]init]; 
[_firstView setTranslatesAutoresizingMaskIntoConstraints:NO]; 
_firstView.backgroundColor = [UIColor grayColor]; 
[self.view addSubview:_firstView]; 

NSLayoutConstraint *firstViewTop = [NSLayoutConstraint constraintWithItem:_firstView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeTop multiplier:1.0 constant:0.0]; 

NSLayoutConstraint *firstViewLeading = [NSLayoutConstraint constraintWithItem:_firstView attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeLeading multiplier:1.0 constant:0.0]; 

NSLayoutConstraint *firstViewTrailing = [NSLayoutConstraint constraintWithItem:_firstView attribute:NSLayoutAttributeTrailing relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeTrailing multiplier:1.0 constant:0.0]; 

NSLayoutConstraint *firstViewHeight = [NSLayoutConstraint constraintWithItem:_firstView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeHeight multiplier:0.15 constant:0.0]; 

NSArray *firstViewConstraints = @[firstViewTop, firstViewLeading, firstViewTrailing, firstViewHeight]; 
[self.view addConstraints:firstViewConstraints]; 

_secondView = [[UIView alloc]init]; 
_secondView.backgroundColor = [UIColor darkGrayColor]; 
[self.secondView setTranslatesAutoresizingMaskIntoConstraints:NO]; 
[self.view addSubview:_secondView]; 

NSLayoutConstraint *secondViewTop = [NSLayoutConstraint constraintWithItem:_secondView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:_firstView attribute:NSLayoutAttributeBottom multiplier:1.0 constant:0.0]; 

NSLayoutConstraint *secondViewLeading = [NSLayoutConstraint constraintWithItem:_secondView attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeLeading multiplier:1.0 constant:0.0]; 

NSLayoutConstraint *secondViewTrailing = [NSLayoutConstraint constraintWithItem:_secondView attribute:NSLayoutAttributeTrailing relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeTrailing multiplier:1.0 constant:0.0]; 

NSLayoutConstraint *secondViewHeight = [NSLayoutConstraint constraintWithItem:_secondView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeHeight multiplier:0.25 constant:0.0]; 

NSArray *secondViewConstraints = @[secondViewTop, secondViewLeading, secondViewTrailing, secondViewHeight]; 
[self.view addConstraints:secondViewConstraints]; 


_thirdView = [[UIView alloc]init]; 
_thirdView.backgroundColor = [UIColor lightGrayColor]; 
[_thirdView setTranslatesAutoresizingMaskIntoConstraints:NO]; 
[self.view addSubview:_thirdView]; 


NSLayoutConstraint *thirdViewTop = [NSLayoutConstraint constraintWithItem:_thirdView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:_secondView attribute:NSLayoutAttributeBottom multiplier:1.0 constant:0.0]; 

NSLayoutConstraint *thirdViewLeading = [NSLayoutConstraint constraintWithItem:_thirdView attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeLeading multiplier:1.0 constant:0.0]; 

NSLayoutConstraint *thirdViewTrailing = [NSLayoutConstraint constraintWithItem:_thirdView attribute:NSLayoutAttributeTrailing relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeTrailing multiplier:1.0 constant:0.0]; 

NSLayoutConstraint *thirdViewHeight = [NSLayoutConstraint constraintWithItem:_thirdView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeHeight multiplier:0.40 constant:0.0]; 

NSArray *thirdViewConstraints = @[thirdViewTop, thirdViewLeading, thirdViewTrailing, thirdViewHeight]; 
[self.view addConstraints:thirdViewConstraints]; 


_fourthView = [[UIView alloc]init]; 
_fourthView.backgroundColor = [UIColor brownColor]; 
[_fourthView setTranslatesAutoresizingMaskIntoConstraints:NO]; 
[self.view addSubview:_fourthView]; 


NSLayoutConstraint *fourthViewTop = [NSLayoutConstraint constraintWithItem:_fourthView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:_thirdView attribute:NSLayoutAttributeBottom multiplier:1.0 constant:0.0]; 

NSLayoutConstraint *fourthViewLeading = [NSLayoutConstraint constraintWithItem:_fourthView attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeLeading multiplier:1.0 constant:0.0]; 

NSLayoutConstraint *fourthViewTrailing = [NSLayoutConstraint constraintWithItem:_fourthView attribute:NSLayoutAttributeTrailing relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeTrailing multiplier:1.0 constant:0.0]; 

NSLayoutConstraint *fourthViewHeight = [NSLayoutConstraint constraintWithItem:_fourthView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeHeight multiplier:0.20 constant:0.0]; 

NSArray *fourthViewConstraints = @[fourthViewTop, fourthViewLeading, fourthViewTrailing, fourthViewHeight]; 
[self.view addConstraints:fourthViewConstraints]; 
} 

 Смежные вопросы

  • Нет связанных вопросов^_^