Add a UIButton as a subview to a UITabBar - ios

I am trying to implement a hideable UITabBar in my app. I've set up all the animations, and they work very well. I'm just having an issue getting my UIButton "pull-tab" to show the tab bar. It is not responding to the touch event UIControlEventTouchUpInside.
I add the pull-tab to the UITabBar in the UITabBarController:
- (void)viewDidLoad
{
[super viewDidLoad];
//Add pull
pullButton = [UIButton buttonWithType:UIButtonTypeCustom];
UIImage *image = [UIImage imageNamed:#"TabBarPull.png"];
pullButton.frame = CGRectMake(self.tabBar.frame.size.width - image.size.width, -image.size.height + 3, image.size.width, image.size.height);
[pullButton setImage:image forState:UIControlStateNormal];
[pullButton addTarget:self action:#selector(pullBarTapped:) forControlEvents:UIControlEventTouchUpInside];
pullButton.userInteractionEnabled = YES;
[self.tabBar addSubview:pullButton];
}
Here is what the tab bar looks like open and closed:
Edit: I've determined that the problem is because the button falls outside the UITabBar's frame. Looks like I'm going to have to put the button outside of the UITabBar... Animation nightmare.

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

Since you have the hiding part working within the UITabbar and from the answer i have seen here, one alternative would be to keep the UIButton within the UITabbar but also add the button to the view when the UITabbar is hidden (so you will have two button that overlay). When the tabbar is displayed hide the button you have added to the view using the hidden property on the view.

Related

TabBarController does not hide the subviews in its view when it hides bottom bar on push

I am trying to make an app that is based on a TabBarController which has a custom button in the middle of the tabbar that is like the one in Instagram and other apps alike.
The reason I know it will be a custom button is that when user presses the button he/she should not expect a 'view controllers' segue behavior but a presented view controller. As to my research by far, it is hard to customize a tab bar item to do anything other than switching to another view controllers owned by the tab bar.
Assuming I was right about the button having to be custom (if you know how to add a bar button item onto a tab bar and has it present a vc instead of switching to a vc, please also include it in your answer), I added a custom button to the view of the tab bar controller:
- (void)addPostButton {
button = [UIButton buttonWithType:UIButtonTypeCustom];
button.autoresizingMask = UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleTopMargin;
button.frame = CGRectMake(0.0, 0.0, 55, 55);
[button setImage:[UIImage imageNamed:#"post.png"] forState:UIControlStateNormal];
[button setBackgroundColor:[UIColor colorWithRed:32.0/255.0 green:35.0/255.0 blue:44.0/255.0 alpha:1]];
[button.layer setCornerRadius:button.frame.size.height / 2];
[button.layer setBorderColor:[UIColor whiteColor].CGColor];
[button.layer setBorderWidth:1.0f];
[button addTarget:self action:#selector(addPost) forControlEvents:UIControlEventTouchUpInside];
CGFloat heightDifference = 60 - self.tabBar.frame.size.height;
if (heightDifference < 0)
button.center = self.tabBar.center;
else
{
CGPoint center = self.tabBar.center;
center.y = center.y - heightDifference/2.0-5;
button.center = center;
}
[self.tabBar addSubview:button];
}
- (void)viewDidLoad
{
[super viewDidLoad];
[self addPostButton];
}
my storyboard is currently constructed as: --root--> tab bar controller --view controllers--> navigation controller --root--> view controller. Now I am trying to make the navigation controller push a view and would like to hide the tab bar in the pushed view. I checked 'hides bottom bar on push', which worked fine, EXCEPT the added subview is still there.
Apparently, 'hidesBottomBarOnPush' only hides tabbarcontroller.tabbar but not other subviews tabbarcontroller.view holds.
I also tried adding the custom button to tabbarcontroller.tabbar but nothing shows up. So it seemed that I had to add the button to the its view, not its tab bar.And When I pop the vc I just pushed, the added button is beneath the tab bar instead of right above it. Something wrong with the tabbarcontroller's view hierarchy.
If you know any solution to either add a custom button to tabbarcontroller.tabbar, or, hide the tabbarcontroller.view's subviews in an elegant way, I would greatly appreciate it.
This likely is happening because you've added the subview to the tab bar controller, not the tab bar itself. There are several ways to address this depending on how you've added the button.

Tab bar items to have different width

I am creating an iPhone app where i need to show 5 tab bar items . My requirement is I want first tab bar item to have smaller width than other 4 . is it possible and how to go about doing this. Please suggest .
I don't think it's possible to do this. UITabBar and UITabBarItem are two of the least customizable classes in UIKit. UITabBarItem does not even inherit from UIView.
I needed to something similar and ended up rolling my own TabBarController, TabBar and TabBarItem classes. If you only need the basic tab bar functionality it's not too much work. If you want the fancy new iOS 7 transitions it will be a lot more work.
Please check it out custom tab bar: https://github.com/aalittle/ALCustomTabBarController
In this sample, please go to TabBarView.xib file, you can change all the tabs buttons width and height according to your requirement.
Hope, It will may helpful,
:)
Create a subclass from UITabBarController and write this code in viewDidLoad.
-(void)viewDidLoad
{
[super viewDidLoad];
[self addCustomElements];
}
-(void)addCustomElements
{
// Backgroun
UIImageView* bgView = Your backgroun image for tab bar;
bgView.frame = CGRectMake(0, self.view.frame.size.height-50, 320, 50);
[self.view addSubview:bgView];
// Initialise our two images
UIImage *btnImage = default image for your tab item;
UIImage *btnImageSelected = selected image for your tab item;
tab1 = [UIButton buttonWithType:UIButtonTypeCustom]; //Setup the button
tab1.frame = // Set the frame (size and position) of the button)
[tab1 setImage:btnImage forState:UIControlStateNormal];
[btnMore setBackgroundImage:btnImageSelected forState:UIControlStateSelected];
[tab1 setTitle:#"title for 1st button" forState:UIControlStateNormal];
[tab1 setTag:0];
// Add my new buttons to the view
[self.view addSubview:tab1];
//repeat same for remaining button
// Setup event handlers so that the buttonClicked method will respond to the touch up inside event.
[tab1 addTarget:self action:#selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
}
set this subclass to your tab bar controller.

iOS custom shape navigation bar

I want to developer app with a custom navigation bar like in the following images:
I think that i need to subclass UINavigationBar and add button to centre of nav bar, but i don't really know how to make navigation bar look like on image. Can you please give me advice what should i do, links to any kind of documentation would be awesome!
Similar questions about navBar that doesn't helped me:
ios back button in the bar
Use custom Navigation Bar in iOS
Custom Navigation Bar in iOS 5
rogcar
EDIT:
My idea is next: make custom navigation bar height little bigger than default size, and add background image with arrow in it and with some transparency on the edges.
If you want a button (you probably do want) you can achieve it completely by subclassing UINavigationBar. You should remember that height of UINavigationBar is read-only property.
Style but not tappable:
So let's assume we subclass the navigation bar and add button there. You could do this and it will be going look great. For example:
- (void)drawRect:(CGRect)rect
{
self.backgroundColor = [UIColor lightGrayColor];
UIButton *myButton = [[UIButton alloc] initWithFrame:CGRectMake(self.frame.size.width/2-50, 0 , 100, 100)];
[myButton setBackgroundColor:[UIColor lightGrayColor]];
[myButton setTitle:#"Normal" forState:UIControlStateNormal];
[myButton setTitle:#"Highlighted" forState:UIControlStateHighlighted];
[self addSubview:myButton];
[self sendSubviewToBack:myButton];
}
But you will facing a problem that your button is non tapeable below UINvaigationBar. (I post an image on the bottom of the answer)
So there is clearly not a path you want to follow. Don't even try that.
Style but not tappable 2:
You may override this method in your navigation bar subclass
- (CGSize) sizeThatFits:(CGSize)size {
return CGSizeMake(custom_width, custom_height);
}
And then mask it using UIBezierPath for example
The right (tappable) way:
You have to create a view stick to your UINavigationBar. What i will do here (if you want it to every screen) is:
Make a Category of UIViewController which can draw (for example - this is easiest way) UIButton.
Style this 'UIButton' whatever you want (if you want
Pin action to 'UIButton': [btn addTarget:self action:#selector(menuShow:) forControlEvents:UIControlEventTouchUpInside];
menuShow: method should be declare in your category
You can call drawing button every time you want to redraw view controller.
As you can see there there will be two separates View: UINavigationBar and UIButton. This is allow you to set content under this little button and make it tapable.
So why just don't hide navigation bar, and use different view? Because iOS7 ;) When Apple change it in iOS7 for example then you have to rebuild your pseudo NavigationBar, with only additional view, you don't need to do anything.
You do not need to subclass UINavigationBar. Create UIView add to it UIImageView as background with image in the shape you need, add button.
Subclass UINavigationController hide UINavigationBar, add custom navigation bar.
First Hide navigation bar using -
self.navigationController.navigationBarHidden = YES;
Then create UIView with required height,height of navigationBar is 44px.Then create background image view, object of required UIButton and add all objects on created UIView as a subview.It will look like navigationBar.Thank you.
You can add your custom shaped view as titleView on the navigation bar.
Just make sure that clipsToBounds is set to NO, so it doesn't get clipped.

Anyone know how to remove unwanted animation from navigation bar? Screenshots

I have a view controller which proceeds via a button to a table view controller.
In the view controller, the navigation bar is totally translucent (As you can see in the screen shot below). On the table view controller, the navigation bar is set to white.
My problem is, when I press 'back' in the table view and return to the view controller, the white navigation bar is carried over for a moment (see top image) before disappearing in an ugly animation.
Extra Navigation bar space:
How I want it to always look:
I have tried pretty much everything I can think of, all my code relating to the navigation bars translucency is in viewDidAppear, so why is this happening!?
Someone please tell me what I'm doing wrong! This is making me crazy!
In the tableviewcontroller set:
- (void)viewWillDisappear:(BOOL)animated {
// put the code for the uinavigation bar styling here.
}
You can do some thing like this have a custom back button below is the code
UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
backButton.frame = CGRectMake(2, 1, 29, 29);
[backButton setBackgroundImage:[UIImage imageNamed:#"back_button"] forState:UIControlStateNormal];
[backButton addTarget:self action:#selector(backButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
backButton.adjustsImageWhenHighlighted = NO
item.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
where item is instance of UINavigationItem
and in selector of back button
-(void)backButtonClicked:(id)sender {
[self.navigationBar popNavigationItemAnimated:NO];
}
by doing this the navigation bar will pop the item but without animation.

Custom right side navigation bar button

I added a right bar button to the tableView in the storyboard, created an IB action which opens another window. Everything works fine. But now I want to use a custom Image for that button. I found this:
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setImage:infoIconImage forState:UIControlStateNormal];
Which I guess with a few extra lines of code will do the job,
but I can't seem to understand what name that button has in the code. How I relate that button from the storyboard to the code.
Beginner question, sorry.
Create an IBOutlet just like you linked your action from your storyboard to your .h file. Name it button, and you're good to go !
BTW, you can set "standard" images in your storyboard : select the button, go to the Attribute Inspector in the Utilities bar of Xcode (right bar), and change the Identifier !
Edit :
Have a look over there to use your own image :
Customize UIBarButtonItem
The part about the UIBarButtonItem is near the end of the article.
OK, I found there is a property customView. SO here it is:
// Set the custom back button
UIImage *infoIconImage = [UIImage imageNamed:#"info-button.png"];
//create the button and assign the image
UIButton *iButton = [UIButton buttonWithType:UIButtonTypeCustom];
[iButton setImage:infoIconImage forState:UIControlStateNormal];
//set the frame of the button to the size of the image
iButton.frame = CGRectMake(0, 0, infoIconImage.size.width, infoIconImage.size.height);
//assigning customized button
infoButton.customView = iButton;

Resources