UIButton隐藏在UIToolbar后面

上下文

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

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

我不添加工具栏作为子视图,我将ViewControllers导航控制器属性toolBarHidden设置为NO。 – [self.navigationController setToolbarHidden:NO animated:NO]; 在视图将apear

额外的细节

我这样做的原因是因为我想创build类似于工具栏(注意:这是一个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到“自定义内容”区域和导航工具栏坐在顶部,无论我做。 见下文:

在这里输入图像说明

虽然我认为最好的解决scheme是以编程方式创buildUIToolbar ,然后创build并添加任何自定义的UIBarButtonItem ,但下面是可能的解决scheme:

注意 :苹果公司说你不能试图修改UINavigationController的默认工具栏 ,所以如果你打算这么做的话,试试上面的build议)

 - (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]; } 

利用这个代码。

  [self.view bringSubviewToFront:yourButton]; 

这应该可以解决你的问题。

使用这个代码

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

那么你的button将在工具栏上方。

 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]; 

这可能会帮助你