iOS ObjectiveC-UIView在旋转回纵向后无法正确显示
我是objective C的新手,在第一次旋转到“横向”后再旋转回“纵向”时,我似乎无法在tableViewCell中设置正确的UIView 我查看了几个相关的帖子,但似乎没有什么能解决我的问题。当我打印宽度时,我得到了预期的结果,尽管输出不正确 我创建了一个UIView“lineView”,以便在tableView中的单元格之间添加自定义行(这是必要的,因为不同部分中存在一些分隔符问题) 下面是部分代码:iOS ObjectiveC-UIView在旋转回纵向后无法正确显示,ios,objective-c,iphone,uiview,rotation,Ios,Objective C,Iphone,Uiview,Rotation,我是objective C的新手,在第一次旋转到“横向”后再旋转回“纵向”时,我似乎无法在tableViewCell中设置正确的UIView 我查看了几个相关的帖子,但似乎没有什么能解决我的问题。当我打印宽度时,我得到了预期的结果,尽管输出不正确 我创建了一个UIView“lineView”,以便在tableView中的单元格之间添加自定义行(这是必要的,因为不同部分中存在一些分隔符问题) 下面是部分代码: - (UITableViewCell *)tableView:(UITableView
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath: (NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"anIdentifier" forIndexPath:indexPath];
... Content of the cell
UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(10, 42,tableView.bounds.size.width-20, 0.7);
lineView.backgroundColor = [UIColor blackColor];
[cell.contentView addSubview:lineView];
return cell;
}
当我运行整个程序时,我得到以下输出(从左到右:无旋转、旋转到横向、旋转回纵向):
图1和图2中的分隔线是正确的,但当我旋转回横向时,右侧的插图消失了。可能有一个微不足道的解决方案,但我的原始技能让我失望
我错过了什么 以下是我如何修复我的问题(感谢@codedad的评论),以防任何人遇到类似问题:
(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath: (NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"anIdentifier" forIndexPath:indexPath];
... Content of the cell
UIView *lineView = [[UIView alloc] init];
lineView.translatesAutoresizingMaskIntoConstraints=NO;
[cell.contentView addSubview:lineView];
NSArray *verticalConstraints =
[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-41.5-[lineView(0.8)]"
options: 0
metrics:nil
views:NSDictionaryOfVariableBindings(lineView)];
NSArray *horizontalConstraints =
[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-20-[lineView(>=50)]-20-|"
options: 0
metrics:nil
views:NSDictionaryOfVariableBindings(lineView)];
[cell.contentView addConstraints:verticalConstraints];
[cell.contentView addConstraints:horizontalConstraints];
}
您还可以使用“constraintsWithItem”。我尝试了两种方法,它们都很好()
我仍然不明白为什么使用“框架”会导致最初的问题,但是使用NSLayoutConstraintClass似乎是一种更安全、更可靠的方法 以下是我如何修复我的问题(感谢@codedad的评论),以防任何人遇到类似问题:
(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath: (NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"anIdentifier" forIndexPath:indexPath];
... Content of the cell
UIView *lineView = [[UIView alloc] init];
lineView.translatesAutoresizingMaskIntoConstraints=NO;
[cell.contentView addSubview:lineView];
NSArray *verticalConstraints =
[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-41.5-[lineView(0.8)]"
options: 0
metrics:nil
views:NSDictionaryOfVariableBindings(lineView)];
NSArray *horizontalConstraints =
[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-20-[lineView(>=50)]-20-|"
options: 0
metrics:nil
views:NSDictionaryOfVariableBindings(lineView)];
[cell.contentView addConstraints:verticalConstraints];
[cell.contentView addConstraints:horizontalConstraints];
}
您还可以使用“constraintsWithItem”。我尝试了两种方法,它们都很好()
我仍然不明白为什么使用“框架”会导致最初的问题,但是使用NSLayoutConstraintClass似乎是一种更安全、更可靠的方法 是将约束应用于该视图的选项吗?我的意思是,例如:[lineView addConstraints:[NSLayoutConstraintsWithVisualFormat:@“H:[lineView(=0.7]”选项:0指标:零视图:NSDictionaryOfVariableBindings(myView)]];宽度也一样。谢谢!这绝对是一个选项。我尝试了VisualFormat和constraintsWithItem。两种方法都可以在代码中进行一些修改后使用。我将在答案中添加代码。这是一个将约束应用于分隔符UIView的选项吗?我的意思是:[lineView addConstraints:[NSLayoutConstraintsWithVisualFormat:@“H:[lineView(=0.7]”选项:0指标:无视图:NSDictionaryOfVariableBindings(myView)]];宽度也一样。谢谢!这绝对是一个选项。我尝试了VisualFormat的Contraint以及Item的constraintsWithItem。这两种方法都可以在代码中进行一些修改后使用。我将在答案中添加代码。