隐藏在 UIToolbar 后面的 UIButton [英] UIButton hidden behind UIToolbar

查看:32
本文介绍了隐藏在 UIToolbar 后面的 UIButton的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

背景

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

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

我没有将工具栏添加为子视图,而是将 ViewControllers navigationController 属性 toolBarHidden 设置为 NO.- [self.navigationController setToolbarHidden:NO animation:NO]; at viewWill

额外细节

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

我试图遵循这个(注意:这也是一个 UITabBar),但很挣扎:,所以如果您打算这样做,请尝试上述建议)

- (void)viewDidLoad{[超级viewDidLoad];//显示默认工具栏[self.navigationController setToolbarHidden:NO];//创建一个 UIButtonUIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];[button setImage:[UIImage imageNamed:@"btn"] forState:UIControlStateNormal];[按钮大小调整];//添加你的目标/动作[button addTarget:self action:@selector(doSomething:) forControlEvents:UIControlEventTouchUpInside];//定位按钮//我确定一定有//一百万种更好的方法来做到这一点//(这里只是为了说明这一点)button.center = self.navigationController.toolbar.center;CGRect 框架 = button.frame;frame.origin.y = CGRectGetMaxY([UIScreen mainScreen].bounds) - button.frame.size.height;button.frame = 框架;//这是棘手的部分.工具栏不是一部分//在控制器的视图层次结构中,它属于//到导航控制器.所以在那里添加按钮[self.navigationController.view addSubview:button];}

Context

I've got a UIButton that I'm adding, method is below:

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

I don't add the toolbar as a subview, I set the ViewControllers navigationController property toolBarHidden to NO. - [self.navigationController setToolbarHidden:NO animated:NO]; at viewWill apear

Extra Detail

The reason I'm doing this is because I want to create something like the toolbar (NOTE: This is a UITabBar, but I'm looking for the same shape) below - so I'm adding a toolbar and then adding a UIButton to the viewControllers view and using the toolbars coordinates to position the UIButton

I've tried to follow along to this (NOTE: Again this is for a UITabBar), but struggling: http://idevrecipes.com/2010/12/16/raised-center-tab-bar-button/

Problem

The UIButton is hidden behing the UIToolbar (I want it to sit on top of the toolbar).

Questions

  • How do I solve this?
  • Would it be a better approach to convert a UIButton to a UIBarButtItem? And add a UIButton to a viewController's toolbarItems array?
  • How would I do this?

Update

After lots of attempts at fixing this, is this something to do with the fact I'm using UINavigationController and the fact that I'm adding a UIButton to the "Custom Content" area and the nav toolbar sits on top of that whatever I do. See below:

解决方案

Although I think that the best solution is to create your UIToolbar programmatically and then create and add any custom UIBarButtonItems, here is a possible solution to your problem:

(Note: Apple says that you must not try to modify UINavigationController's default toolbar, so if you're planning to do so try the above suggestion)

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

这篇关于隐藏在 UIToolbar 后面的 UIButton的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆