UITableView navigationController 部分索引在导航标题后对齐

发布于 2024-09-14 17:34:50 字数 447 浏览 2 评论 0原文

在带有索引的 UITableView 部分中,如何使所选部分的标题(通过按部分索引)在屏幕的最顶部对齐(_not_)(导航控制器下) )而是在顶部的导航栏标题之后齐平?

像这样:


[button] Navigation Bar Header at top of screen [other button]
X (section letter)
items within section....

需要明确的是,我遇到的问题是,当在右侧按下某个部分索引时,表格会将该部分滚动到屏幕的最顶部 >隐藏在导航栏下方。我希望该节标题正好在导航标题之后接触导航栏底部边框与节的顶部字母行 - 例如,当表格首次出现时。

In a Section Indexed UITableView how do you make the title of the section chosen (by pressing the Section Index) align _not_ at the very top of the screen (under the navigation controller) but rather flush after the Navigation Bar Header at the top?

like this:


[button] Navigation Bar Header at top of screen [other button]


X (section letter)


items within section....

To be clear, the problem I am having is that when a section index is pressed on the right, the table scrolls that section to the very top of the screen, hidden underneath the navigation bar. I want that section header precisely after the navigation header touching the Navigation Bar bottom border with the top of the Section Letter row - as it is for example, when the table first appears.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

长途伴 2024-09-21 17:34:50

更好的解决方案:

a) 睡一会儿。

b) 意识到,当您睡觉时,'UIBarStyleBlackTranslucent' 的使用已弃用,因此只需执行以下操作:

  • 打开 MainWindow。在 Interface Builder 中,选择“导航控制器”内的“UINavigationBar”项,然后将“样式”更改为“黑色不透明”,而不是半透明。
  • 在你的 UITableViewController 类文件中,写入

    - (void)viewWillDisappear:(BOOL)animated {
    self.navigationController.navigationBar.translucent = YES; /* 是的,如果您愿意,可以转到导航栏下方的其他位置。 */
    [超级viewWillDisappear:动画];
    }
    - (void)viewWillAppear:(BOOL)动画{
    self.navigationController.navigationBar.translucent = NO; /* 禁止进入导航栏下方。问题解决了! */
    ...
        [超级viewWillAppear:动画];
    }
    

c) 别再感到尴尬了,而是意识到睡眠现在是你的武器库中一种更有用的编码技术。 ;p


The clues to the correct solution:

“导航控制器从不在不透明导航栏下显示内容。”

“将导航控制器的半透明属性设置为 YES。这允许您的内容覆盖导航栏。”

请参阅:developer.apple.com/iphone/library/featuredarticles/ViewControllerPGforiPhoneOS/NavigationControllers/NavigationControllers.html

请参阅:developer.apple.com/iphone/library/documentation/UIKit/Reference/UINavigationBar_Class/Reference/UINavigationBar.html#//apple_ref /occ/instp/UINavigationBar/半透明

UIBarStyle 定义不同类型视图的风格外观...

UIBarStyleBlackTranslucent = 2, // 已弃用

使用 UIBarStyleBlack 并将半透明属性设置为 YES。

http://developer.apple.com/ iphone/library/documentation/uikit/reference/UIKitDataTypesReference/Reference/reference.html


BEFORE SLEEPING, fixed it "The Hard Way"...

- (void)correctFrame:(UIView *)viewWithWrongFrame {
    CGRect correctedFrame = [[viewWithWrongFrame superview] frame];
    correctedFrame.size.height = correctedFrame.size.height - self.navigationController.navigationBar.frame.size.height; /* Height without Navigation Bar */
    correctedFrame.origin.y = correctedFrame.origin.y + self.navigationController.navigationBar.frame.size.height; /* Origin below Navigation Bar */
    [viewWithWrongFrame setFrame:correctedFrame];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    ...
    [self correctFrame:[self.navigationController.view.subviews objectAtIndex:0]]; /* Correct the frame so it is after the Navigation Bar */
    [self.tableView setContentInset:UIEdgeInsetsMake(-self.navigationController.navigationBar.frame.size.height, 0, 0, 0)]; /* Eliminate initial blank space at top.*/
    ...
}

- (void)cleanDisplay {
    [self.tableView setContentInset:UIEdgeInsetsZero]; /* Fix difference between horizontal and vertical orientation. */
    ...
}
- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation duration:(NSTimeInterval)duration {
    [self correctFrame:[self.navigationController.view.subviews objectAtIndex:0]]; /* Correct the frame so it is after the Navigation Bar */

    /* Clean the display after turning... */
    [self performSelectorOnMainThread:@selector(cleanDisplay) withObject:nil waitUntilDone:NO];
}
- (void)viewWillAppear:(BOOL)animated {
    ...
    [self performSelectorOnMainThread:@selector(cleanDisplay) withObject:nil waitUntilDone:NO];
    ...
}

请注意,失败方法包括:

使用 Interface Builder 将 UITableView 嵌入到 UIView 中。不起作用,因为它全部自动调整到完整高度。
调整大小 [self.tableView superview].frame = CGRectInset([self.tableView superview].frame, 44, 44);
或者 .bounds 没有效果,
self.tableView.frame 和 .bounds 都没有。

当我尝试时,出现了问题的线索:
self.navigationController.view.frame = CGRectInset(self.navigationController.view.frame, 20, 44);
这实际上改变了其中包含表的视图的大小。显然,将子视图更改为该子视图可以解决原始问题。

用上面的方法修复了它。尽管在后退或前进到其他屏幕之前,更改需要恢复正常,以避免将元素移动到其他地方。

Better solution:

a) Get some sleep.

b) Realize that, while you were sleeping, the use of 'UIBarStyleBlackTranslucent' has been deprecated, so simply do this instead:

  • Open MainWindow.xib in Interface Builder, select your 'UINavigationBar' item inside the 'Navigation Controller' inside your 'Tab Bar Controller' and change the 'Style' to 'Black Opaque' instead of translucent.
  • In your UITableViewController class file, write

    - (void)viewWillDisappear:(BOOL)animated {
    self.navigationController.navigationBar.translucent = YES; /* Yes, go underneath the Navigation Bar elsewhere if you want to. */
    [super viewWillDisappear:animated];
    }
    - (void)viewWillAppear:(BOOL)animated {
    self.navigationController.navigationBar.translucent = NO; /* No going underneath the Navigation Bar. Problem solved! */
    ...
        [super viewWillAppear:animated];
    }
    

c) Stop feeling embarrassed and instead realize how sleep is now one more useful coding technique in your arsenal. ;p


The clues to the correct solution:

"The navigation controller never displays content under an opaque navigation bar."

"Set the translucent property of your navigation controller to YES. This allows your content to underlap the navigation bar."

see: developer.apple.com/iphone/library/featuredarticles/ViewControllerPGforiPhoneOS/NavigationControllers/NavigationControllers.html

see: developer.apple.com/iphone/library/documentation/UIKit/Reference/UINavigationBar_Class/Reference/UINavigationBar.html#//apple_ref/occ/instp/UINavigationBar/translucent

UIBarStyle Defines the stylistic appearance of different types of views...

UIBarStyleBlackTranslucent = 2, // Deprecated

Use UIBarStyleBlack and set the translucent property to YES instead.

http://developer.apple.com/iphone/library/documentation/uikit/reference/UIKitDataTypesReference/Reference/reference.html


BEFORE SLEEPING, fixed it "The Hard Way"...

- (void)correctFrame:(UIView *)viewWithWrongFrame {
    CGRect correctedFrame = [[viewWithWrongFrame superview] frame];
    correctedFrame.size.height = correctedFrame.size.height - self.navigationController.navigationBar.frame.size.height; /* Height without Navigation Bar */
    correctedFrame.origin.y = correctedFrame.origin.y + self.navigationController.navigationBar.frame.size.height; /* Origin below Navigation Bar */
    [viewWithWrongFrame setFrame:correctedFrame];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    ...
    [self correctFrame:[self.navigationController.view.subviews objectAtIndex:0]]; /* Correct the frame so it is after the Navigation Bar */
    [self.tableView setContentInset:UIEdgeInsetsMake(-self.navigationController.navigationBar.frame.size.height, 0, 0, 0)]; /* Eliminate initial blank space at top.*/
    ...
}

- (void)cleanDisplay {
    [self.tableView setContentInset:UIEdgeInsetsZero]; /* Fix difference between horizontal and vertical orientation. */
    ...
}
- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation duration:(NSTimeInterval)duration {
    [self correctFrame:[self.navigationController.view.subviews objectAtIndex:0]]; /* Correct the frame so it is after the Navigation Bar */

    /* Clean the display after turning... */
    [self performSelectorOnMainThread:@selector(cleanDisplay) withObject:nil waitUntilDone:NO];
}
- (void)viewWillAppear:(BOOL)animated {
    ...
    [self performSelectorOnMainThread:@selector(cleanDisplay) withObject:nil waitUntilDone:NO];
    ...
}

Note that failed approaches included:

Using Interface Builder to embed the UITableView inside a UIView. Did not work because it all got automatically sized to the full height.
Resizing [self.tableView superview].frame = CGRectInset([self.tableView superview].frame, 44, 44);
or .bounds had no effect,
neither did self.tableView.frame nor .bounds.

The clue to what was wrong came when I tried:
self.navigationController.view.frame = CGRectInset(self.navigationController.view.frame, 20, 44);
which actually changed the size of the view with the table in it. Clearly, changing the subview to that would fix the original problem.

Fixed it with the approach further above. Although before moving back or forward to other screens changes need to be reverted to normal to avoid shifting elements elsewhere.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文