【问题标题】:Strange behavior with UIToolbar in iOS appiOS应用程序中UIToolbar的奇怪行为
【发布时间】:2013-11-12 22:03:34
【问题描述】:

在我当前的项目中,我有一个基于导航控制器的应用程序。应用程序中的某些视图需要视图底部的工具栏。我正在使用以下代码以编程方式创建工具栏(摘自 table view controller 中的 viewDidLoad):

UIBarButtonItem *reloadButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh target:self action:@selector(refreshList)];

[reloadButton setTitle:@"Refresh"];
[reloadButton setTintColor:[UIColor whiteColor]];

self.navigationController.toolbar.barStyle = UIBarStyleBlackTranslucent;
[self.navigationController.toolbar sizeToFit];
CGFloat toolbarHeight = [self.navigationController.toolbar frame].size.height;
[self.navigationController.toolbar setFrame:CGRectMake(CGRectGetMinX(self.view.bounds),
                                                       438,
                                                       CGRectGetWidth(self.view.bounds),
                                                       toolbarHeight)];
NSArray *toolbarItems = [[NSArray alloc] initWithObjects:reloadButton, nil];
[self setToolbarItems:toolbarItems];
[self.navigationController setToolbarHidden:NO];

这在大多数视图中运行良好。但是,我现在使用相同的代码将其合并到另一个视图中。在该控制器中,工具栏出现在视图底部上方约一英寸处——它不像在其他视图控制器中那样对齐到视图底部。导航到问题视图后,其他视图中的其他工具栏开始表现出相同的行为。这只发生在模拟器中,而不是我的物理设备上。但是,我目前只有 iPhone 4 作为我的物理设备。这是模拟器中的错误还是某个问题的迹象?谢谢!

【问题讨论】:

  • 会不会是这个特殊的视图控制器隐藏了状态栏?

标签: ios objective-c uinavigationcontroller uitoolbar


【解决方案1】:

您将 y 位置设置为硬编码值 438。这将导致它不会出现在较高的 iPhone 5 的屏幕底部)。您还应该计算 y 位置:

CGRectGetMaxY(self.view.bounds) - toolbarHeight

这将导致:

[self.navigationController.toolbar setFrame:CGRectMake(CGRectGetMinX(self.view.bounds),
                                                   CGRectGetMaxY(self.view.bounds) - toolbarHeight,
                                                   CGRectGetWidth(self.view.bounds),
                                                   toolbarHeight)];

这也可能与导航栏和状态栏的存在有关

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-03
    • 2016-11-02
    • 2020-02-03
    • 1970-01-01
    • 1970-01-01
    • 2018-05-30
    相关资源
    最近更新 更多