0

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.

4

1 回答 1

1

更好的解决方案:

a) 睡一觉。

b) 意识到,当你睡觉时,'UIBarStyleBlackTranslucent'的使用已被弃用,所以只需这样做:

  • 在 Interface Builder 中打开 MainWindow.xib,在“Tab Bar Controller”中的“Navigation Controller”中选择“UINavigationBar”项,并将“Style”更改为“Black Opaque”而不是半透明。
  • 在你的 UITableViewController 类文件中,写

    - (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) 不要感到尴尬,而是要意识到睡眠现在是你武器库中一种更有用的编码技术。;p


正确解决方案的线索:

“导航控制器永远不会在不透明的导航栏下显示内容。”

“将导航控制器的半透明属性设置为 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/translucent

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

UIBarStyleBlackTranslucent = 2, // 已弃用

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

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


睡觉前,用“艰难的方式”修复它......

- (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); 这实际上改变了包含表的视图的大小。显然,将子视图更改为可以解决原始问题。

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

于 2010-08-18T05:01:33.467 回答