简体   繁体   English

iOS界面生成器布局奇怪

[英]ios interface builder layout oddity

I'm using XCode+IB to layout a pretty simple view. 我正在使用XCode + IB来布局一个非常简单的视图。 There is a standard NavigationController navigation bar at the top. 顶部有一个标准NavigationController导航栏。 Inside the main View is another View which contains a Label, and then a table below that takes up the rest of the screen. 在主视图内部是另一个视图,其中包含一个标签,然后是一个下面的表格,该表格占据了屏幕的其余部分。 I put the label in this container view because I want that area below the navigation bar to have a background color (gray). 我将标签放在此容器视图中,因为我希望导航栏下方的区域具有背景色(灰色)。

The problem is that even though the main View starts just below the navigationBar, the label's container view is exists underneath the navigation bar. 问题是,即使主视图仅在NavigationBar下方开始,标签的容器视图仍位于导航栏下方。 Sort of. 有点。 You see, the label appears below the nav bar, but the gray background is somehow under the navigation bar. 您会看到,标签显示在导航栏下方,但灰色背景某种程度上位于导航栏下方。 If I set the height of the label's view to be 75px, it just starts to appear below the navigation bar. 如果我将标签视图的高度设置为75px,它将开始出现在导航栏下方。 (22+44 for statusbar and navigation bar) (状态栏和导航栏为22 + 44)

Auto-layout is disabled. 自动版式已禁用。

So why is the View container starting below the navigation bar for the background? 那么,为什么视图容器从背景导航栏下方开始? (but not its internal label?) (但不是内部标签?)

Since IOS7, I use this in the ViewController so that the content starts below the navigation bar, instead of underneath it. 从IOS7开始,我在ViewController中使用它,以便内容从导航栏下方而不是其下方开始。

if ([self respondsToSelector:@selector(edgesForExtendedLayout)]){
    self.edgesForExtendedLayout = UIRectEdgeNone;
}

I realize alternatively I can set the main view's background or make the label especially large perhaps and give it a background, but I'd like to see why this isn't working first. 我意识到,我可以设置主视图的背景,或者使标签特别大并为其提供背景,但是我想知道为什么它不能首先起作用。

EDIT: I added a feature to this View where the main view will shift upwards 125px when the keyboard is opened for some UITextFields. 编辑:我向此视图添加了一个功能,当为某些UITextFields打开键盘时,主视图将向上移动125px。 Interestingly, the entire view shifted except for the Label's View container. 有趣的是,除了Label的View容器之外,整个视图都发生了变化。 That view remained in place while it's parent view shifted up and back. 当父视图前后移动时,该视图保持不变。 The label itself shifted up and down however. 标签本身会上下移动。 So once again, the middle View seems linked to the Window, and not the actual main View... 因此,再次,中间视图似乎链接到窗口,而不是实际的主视图。

IB

在此处输入图片说明

Check if you have Adjust Scroll view Insets on! 检查您是否已打开“调整滚动”视图插图!

Go to your view controller, and it is under the Attributes Inspector 转到视图控制器,它在“属性”检查器下

You might want to consider a header cell if you just want to be able to post a message or instructions just below the nav bar. 如果您只想在导航栏下方发布消息或说明,则可能需要考虑标题单元格。 You can then avoid the extra UIView thing. 然后,您可以避免多余的UIView。 The header cell will not scroll with the table view if that is your concern and it will position just below nav and above the rest of the table cells. 如果您担心的话,标题单元格将不会与表格视图一起滚动,并且将位于导航栏的正下方和其余表格单元格的上方。 Here is a simplified example that uses a custom header cell setting the background color. 这是一个使用自定义标题单元格设置背景颜色的简化示例。 You can also drop UILabels, etc. into the cell via IB if you choose. 如果选择,还可以通过IB将UILabel等放入单元格中。

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{  
    MyCustomHeaderCell *headerCell = [tableView dequeueReusableCellWithIdentifier:@"MyCustomHeaderCell"];
    headerCell.backgroundColor = [UIColor grayColor];
    return headerCell;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM