【问题标题】:Help Fixing UIViewController with a UIToolbar使用 UIToolbar 帮助修复 UIViewController
【发布时间】:2010-08-04 02:19:33
【问题描述】:

我有一个顶部有导航栏的应用。在作为 UITableView 子类的一个视图中,我使用以下代码在 UITableView 下方添加了一个 UIToolbar:

UIToolbar *toolbar = [[UIToolbar alloc] init];
[toolbar sizeToFit]; // Set the toolbar to fit the width of the app
CGFloat toolbarHeight = [toolbar frame].size.height; // Calculate the height of the toolbar
CGRect rootViewBounds = self.parentViewController.view.bounds;
CGFloat rootViewHeight = CGRectGetHeight(rootViewBounds);
CGFloat rootViewWidth = CGRectGetWidth(rootViewBounds);
CGRect rectArea = CGRectMake(0, toolbarHeight, rootViewWidth, toolbarHeight);
[toolbar setFrame:rectArea];
[self.navigationController.view addSubview:toolbar];

问题在于工具栏位于 UITableView 的“顶部”,并且遮盖了 UITableView 第一行内容的顶部。我真正想要的是表格视图在 UIToolbar 下方“开始”。

我如何正确地完成这项工作?

感谢, 何塞

【问题讨论】:

    标签: iphone objective-c


    【解决方案1】:

    我同意 nonamelive,但如果有理由需要您描述的逻辑。您应该使视图成为具有 UITableView 子视图和工具栏子视图的普通视图,并相应地设置它们的框架以根据需要定位它们。

    如果您希望工具栏与其他表格内容一起滚动,另一个选项是将表格的 headerView 设为工具栏。

    【讨论】:

      【解决方案2】:

      在我看来,初始化一个新工具栏是错误的做法。

      只需使用这个简单的代码,因为您已经有了 UINavigationController。

      - (void)viewDidLoad {
          self.navigationController.toolbar.hidden = NO;
      }
      

      我很抱歉错误的代码。试试这个吧!

      - (void)viewDidLoad {
          self.navigationController.toolbarHidden = NO;
      }
      

      【讨论】:

      • 嗯。这在viewDidLoad 中没有做任何事情,但是如果我将此代码附加到按钮上,那么它确实可以工作。为什么会这样?
      猜你喜欢
      • 2014-07-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多