iOS ObjectiveC - 旋转回纵向后 UIView 无法正确显示
iOS ObjectiveC - UIView not show correctly after rotating BACK to Portrait
我是 objective C 的新手,在第一次旋转为横向后旋转回纵向时,我似乎无法在 tableViewCell 中设置正确的 UIView。
我查看了几个相关的帖子,但似乎都没有解决我的问题。当我打印宽度时,虽然输出不正确,但我得到了预期的结果。
我创建了一个 UIView "lineView" 以在我的 tableView 中的单元格之间添加自定义行(这是必要的,因为不同部分中存在一些分隔符问题)。
部分代码如下:
- (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'。我尝试了这两种方法,它们都工作正常 (https://developer.apple.com/library/prerelease/ios/documentation/AppKit/Reference/NSLayoutConstraint_Class/index.html)。
我仍然不明白为什么使用 'frame' 会导致最初的问题,但无论如何使用 NSLayoutconstrainClass 似乎是一种更安全、更可靠的方法。
我是 objective C 的新手,在第一次旋转为横向后旋转回纵向时,我似乎无法在 tableViewCell 中设置正确的 UIView。
我查看了几个相关的帖子,但似乎都没有解决我的问题。当我打印宽度时,虽然输出不正确,但我得到了预期的结果。
我创建了一个 UIView "lineView" 以在我的 tableView 中的单元格之间添加自定义行(这是必要的,因为不同部分中存在一些分隔符问题)。
部分代码如下:
- (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'。我尝试了这两种方法,它们都工作正常 (https://developer.apple.com/library/prerelease/ios/documentation/AppKit/Reference/NSLayoutConstraint_Class/index.html)。
我仍然不明白为什么使用 'frame' 会导致最初的问题,但无论如何使用 NSLayoutconstrainClass 似乎是一种更安全、更可靠的方法。