0

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

This is the form I want to create

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:

enter image description here

drc
  • 1,905
  • 4
  • 22
  • 28

4 Answers4

0

Make use of this code.

  [self.view bringSubviewToFront:yourButton];

That should solve your problem.

Vinayak Kini
  • 2,919
  • 21
  • 35
  • That doesn't seem to be working, let me follow up with more detail tomorrow. – drc Apr 26 '13 at 04:03
  • I don't add the toolbar as a subview, I set the ViewControllers navigationController property toolBatHidden to NO. - [self.navigationController setToolbarHidden:NO animated:NO]; at viewWill apear – drc Apr 26 '13 at 13:29
0

use this codes

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

then your button will be above the toolbar.

shanegao
  • 3,562
  • 1
  • 18
  • 21
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];

this may help you

ray1th
  • 64
  • 1
  • 6
0

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];
}
Alladinian
  • 34,483
  • 6
  • 89
  • 91
  • It's still sitting behind. Continue to dig. – drc Apr 26 '13 at 15:17
  • @drc I'm positive that this works, I have tested the code before posting the answer. – Alladinian Apr 26 '13 at 15:18
  • I'll continue to try to get it to work. Thanks for your help so far and your time, appreciate it. – drc Apr 26 '13 at 15:26
  • can you clarify your comment around Apple guideline on not modifying the "default toolbar", I am planning on having a custom toolbar in the app, and I was creating a UIToolBar and adding it a subview of the UIViewController, however I reverted back to using navigationController setToolBarHidden NO as I was struggling to keep the toolbar @ the right Y pos. The app also hides and shows a nav bar in a certain context and when trying to support 4inch screen height it got too complex to keep setting the "custom" toolbar's frame. – drc Apr 26 '13 at 15:33
  • Are you saying that creating a UIToolbar and adding it as a subview of the ViewControllers view is a custom toolbar created programmatically. And that setting the ViewControllers property - toolBarHidden to NO means you are using the "default" toolbar. – drc Apr 26 '13 at 15:34
  • Yes exactly. I think that the best solution would be to create one programmatically and add it to navigation controller's view hierarchy to maintain persistence. – Alladinian Apr 26 '13 at 15:36
  • Also note that when you're using the default one, the right way to add items to it is to call `setToolbarItems:` on each of your controllers, passing the respective items. – Alladinian Apr 26 '13 at 15:42
  • I was using that but it wasn't working. See http://stackoverflow.com/questions/16219160/subclassing-uitoolbar-and-overriding-drawrect-uibarbuttonitems-not-displaying – drc Apr 26 '13 at 15:49
  • ugg. I had the toolbar sit to visible after adding the custom button. I swapped the order and this now fixed my issue. – drc Apr 26 '13 at 16:59