0

语境

我有一个要添加的 UIButton,方法如下:

[self.view addSubview:[self returnCustomSubmitButton]];

我没有将工具栏添加为子视图,而是将 ViewControllers navigationController 属性 toolBarHidden 设置为 NO。-[self.navigationController setToolbarHidden:NO animated:NO];在视图中会出现

额外细节

我这样做的原因是因为我想在下面创建类似于工具栏的东西(注意:这是一个 UITabBar,但我正在寻找相同的形状) - 所以我要添加一个工具栏,然后添加一个 UIButton viewControllers 视图并使用工具栏坐标定位 UIButton

这是我要创建的表格

我试图遵循这一点(注意:这同样适用于 UITabBar),但正在苦苦挣扎:http: //idevrecipes.com/2010/12/16/raised-center-tab-bar-button/

问题

UIButton 隐藏在 UIToolbar 后面(我希望它位于工具栏的顶部)。

问题

  • 我该如何解决这个问题?
  • 将 UIButton 转换为 UIBarButtItem 会更好吗?并将 UIButton 添加到 viewController 的 toolbarItems 数组?
  • 我该怎么做?

更新

经过大量尝试解决此问题后,这是否与我正在使用 UINavigationController 以及我将 UIButton 添加到“自定义内容”区域并且导航工具栏位于其顶部的事实有关我做什么. 见下文:

在此处输入图像描述

4

4 回答 4

0

使用此代码。

  [self.view bringSubviewToFront:yourButton];

那应该可以解决您的问题。

于 2013-04-26T03:53:56.317 回答
0

虽然我认为最好的解决方案是以UIToolbar编程方式创建您的,然后创建并添加任何自定义UIBarButtonItems,但这是您的问题的可能解决方案:

注意:Apple 表示您不得尝试修改UINavigationController的默认工具栏,因此如果您打算这样做,请尝试上述建议)

- (void)viewDidLoad
{
    [super viewDidLoad];

    // Show the default toolbar
    [self.navigationController setToolbarHidden:NO];

    // Create a UIButton
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setImage:[UIImage imageNamed:@"btn"] forState:UIControlStateNormal];
    [button sizeToFit];

    // Add your targets/actions
    [button addTarget:self action:@selector(doSomething:) forControlEvents:UIControlEventTouchUpInside];

    // Position the button
    // I am sure that there must be
    // a million better ways to do this
    // (it's here just to illustrate the point)
    button.center = self.navigationController.toolbar.center;
    CGRect frame = button.frame;
    frame.origin.y = CGRectGetMaxY([UIScreen mainScreen].bounds) - button.frame.size.height;
    button.frame = frame;

    // Here is the tricky part. Toolbar is not part
    // of your controller's view hierarchy, it belongs
    // to the navigation controller. So add the button there 
    [self.navigationController.view addSubview:button];
}
于 2013-04-26T14:27:15.407 回答
0

使用此代码

[self.view insertSubview:[self returnCustomSubmitButton] aboveSubview:toolbar];

那么您的按钮将位于工具栏上方。

于 2013-04-26T04:03:17.017 回答
0
UIToolbar *toolbar = [[UIToolbar alloc] init];
toolbar.frame = CGRectMake(0, 0, self.view.frame.size.width, 44);
NSMutableArray *items = [[NSMutableArray alloc] initWithObjects:your buttons, nil]
[toolbar setItems:items];
[self.view addSubview:toolbar];

这可能会帮助你

于 2013-04-26T06:55:14.487 回答