Add a UIbutton as a Subview to a UItabbar

Add a UIButton as a subview to a UITabBar

You can still add the UIButton to the UITabBarController's main view, not in the UITabBar though.... [myUITabBarController.view addSubview:pullButton]

Adding UIButton to UITabBar

You cannot add a UIButton directly to a UITabBarController. From the UITabBarController Class Reference:

You should never access the tab bar view of a tab bar controller
directly.

Further along, the documentation reads:

Tab bar items are configured through their corresponding view
controller. To associate a tab bar item with a view controller, create
a new instance of the UITabBarItem class, configure it appropriately
for the view controller, and assign it to the view controller’s
tabBarItem property. If you do not provide a custom tab bar item for
your view controller, the view controller creates a default item
containing no image and the text from the view controller’s title
property.

Therefore your two options are:

  1. Create a UITabBarItem (documentation) and add that.
  2. Use a UIToolBar instead, but remember to add UIBarButtonItems.

Placing a UIButton in the same space as UITabBar (When hidden)

Tander I finally figured it out, The button was not responding because its not receiving our touch events, you see In our storyboard, the View is not connected to the viewController, and due to this when I add the button programmatically to View It would never respond, A simple line solved this

In the function

- (IBAction)doneButton:(UIBarButtonItem *)sender

Just do this

- (IBAction)doneButton:(UIBarButtonItem *)sender
{
/* Will hide or unhide the tabBar */

self.tabBarController.tabBar.hidden=YES;
[self.tabBarController setTabBarItem:nil];

UIButton *butt=[UIButton buttonWithType:UIButtonTypeCustom];
butt.backgroundColor=[UIColor redColor];
butt.frame=CGRectMake(160, 530, 93, 40);
[butt addTarget:self action:@selector(testButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
[self.tabBarController.view addSubview:butt];

}

the self.tabBarController.view was our culprit, not letting the touch Events pas through, glad to have figured it out

UIButton going behind Tab Bar when navigating back to parent View Controller - iOS

Okay, found the solution! instead of hiding the tabBar by

- (BOOL)hidesBottomBarWhenPushed {
return YES;
}

hide the tabBar by [tabBarController.tabBar setHidden:YES] in the last viewController. Then when navigate back, do [tabBarController.tabBar setHidden:NO]; in viewWillDisappear.

This way the subviews arrangement in tabBarController stays same :)

How to push a ViewController from a UIButton that is embedded in Tab Bar Controller

So I figured it out. Turns out it was much simpler than I thought, which makes me think I was explaining myself poorly in the question. Anyway, here's the code that worked for me.

let myViewController = ViewController()
let nav = UINavigationController(rootViewController: myViewController)

self.present(nav, animated:true, completion: nil)


Related Topics



Leave a reply



Submit