Я хочу установить ограничения ширины программно, используя Objective-C.
Приведенный ниже код работает нормально, когда я создаю свое приложение, используя Xcode 7.2.
[secHeaderView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[_sectionHeaderView]|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(_sectionHeaderView)]];
[secHeaderView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[_sectionHeaderView]|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(_sectionHeaderView)]];
Теперь, когда я запускаю свое приложение, используя Xcode 9.3
, мои secHeaderView отсутствуют.Это из-за ограничений, которые упомянуты выше.
Если я закомментирую вышеупомянутые ограничения, то я смогу увидеть мой secHeader View, но ширина не установлена должным образом.Он показывает 70% ширины UIScreen.
Так выглядит мой secHeaderView, но ширина не установлена должным образом ...
![enter image description here](https://i.stack.imgur.com/92AmI.png)
Вот мой фактический код:
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
_sectionHeaderView = [[NSBundle mainBundle] loadNibNamed:@"BasicProdInfoSectionHeaderView" owner:self options:nil].firstObject;
SectionHeader *currentHeader = [self.titleStringArray objectAtIndex:section];
[_sectionHeaderView setTitleWithText:currentHeader.titleLabel];
_sectionHeaderView.expandableButton.tag = section;
[_sectionHeaderView.expandableButton addTarget:self action:@selector(expandButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
UIView *secHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0,[[UIScreen mainScreen] bounds].size.width , 50)];
_sectionHeaderView.translatesAutoresizingMaskIntoConstraints = NO;
[secHeaderView addSubview:_sectionHeaderView];
[secHeaderView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[_sectionHeaderView]|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(_sectionHeaderView)]];
[secHeaderView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[_sectionHeaderView]|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(_sectionHeaderView)]];
if (section != 3) {
secHeaderView.layer.shadowOffset = CGSizeMake(0, 1);
secHeaderView.layer.shadowRadius = 1;
[secHeaderView.layer setShadowColor:[UIColor blackColor].CGColor];
secHeaderView.layer.shadowOpacity = 0.25;
}
return secHeaderView;
}