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.
Related
I am trying to add a custom bar button to my project and I'm having a few problems. First lets go over the requirement for my button, it needs:
- to display a custom font
- to be enabled/disabled
- to have its colour animated based to provide user feedback.
So I first tried connecting a barButtonItem up through the storyboard and I could change the colour and enable disable it. However I could not change the font or add colour animation to it. I want to be able to use the following method which I am already using in the app to animate the colour:
- (void)textFlashTextfieldAnimation:(nonnull UITextField *)view duration:(NSTimeInterval)duration animateToColour:(UIColor*)animationColour textColor:(UIColor*)textColour completion:(void (^)(BOOL finished))completion {
[UIView transitionWithView:view duration:0 options:UIViewAnimationOptionTransitionCrossDissolve animations:^{
view.textColor = animationColour;
} completion:^(BOOL finished) {
[UIView transitionWithView:view duration:2.0f options:UIViewAnimationOptionTransitionCrossDissolve animations:^{
view.textColor = textColour;
} completion:completion];
}];
}
Ideally I would be able to change this function to accept a barButtonItem / UIButton so that I could animate it easily. However if I connect the button up through storyboards I cannot access the button's layer so the animation is not possible.
I had started to go about this through code but I've gone down 2 dead ends. Here is a really basic implementation of a barButtonItem:
UIBarButtonItem *myButton=[[UIBarButtonItem alloc] initWithTitle:#"My Custom Button" style:UIBarButtonItemStylePlain target:self action:#selector(/*do something*/)];
[self.navigationItem setRightBarButtonItem:myButton];
Now this will work in the fact that I can use it like a storyboard button however I cannot alter the font or access the button's layer so it is also no good.
I then tried creating a fully custom view for my bar button by creating a UIView and a UIButton and then adding them to a barButtonItem, this can be seen here:
UIFont *barButtonFonts = [UIFont fontWithName:kFont size:16];
//Right Button
//create view
UIView *rightButtonView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 45, 25)];
//create button
UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeSystem];
rightButton.backgroundColor = [UIColor clearColor];
rightButton.frame = rightButtonView.frame; //set frame = to view
[rightButton setTitle:NSLocalizedString(#"Save", nil) forState:UIControlStateNormal];
rightButton.titleLabel.font = barButtonFonts;
rightButton.tintColor = [UIColor redColor];
[rightButton addTarget:self action:#selector(actionMethod) forControlEvents:UIControlEventTouchUpInside];
[rightButtonView addSubview:rightButton];
UIBarButtonItem *rightBarButton = [[UIBarButtonItem alloc] initWithCustomView:rightButtonView];
self.navigationItem.rightBarButtonItem = rightBarButton;
Now I have a barButtonItem that will display a custom font however it is has no UI interaction at all. It looks the same when enabled/disabled and when not-highlighted/highlighted. The button works as it should it just doesn't look like it should.
Should I create an outlet to this button somewhere in my code so that I can alter the button when in the view? Is it even possible to alter a button after it has been added to the navigation bar?
If anybody has any suggestions it would be appreciated!
Yeah barbuttonitem isn't modifieable after you added it to the bar. only way i can think is to remove the button and add a new one (with the new characteristics) when user interact with it. I suggest you to use a custom toolbar (like a normal UIView), then add a normal button to it. it would be easier to customize
I tried to make a navigation back button which has the same complete style as system, but no matter how I adjust it still had a little difference.
system
custom
You can see no matter the arrow or the word (返回 BACK) both have some difference.
Here is the code I tried and I got the arrow image from here.
UIImage *sourceImage = [UIImage imageNamed:#"nav-back-btn"];
UIButton *backBtn = [UIButton buttonWithType:UIButtonTypePlain];
[backBtn setImage:sourceImage forState:UIControlStateNormal];
[backBtn setTitle:#" 返回" forState:UIControlStateNormal];
[backBtn addTarget:self action:#selector(goPreviosPage) forControlEvents:UIControlEventTouchUpInside];
backButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backBtn];
backBtn.transform = CGAffineTransformScale(CGAffineTransformMakeTranslation(-8, 0), 1.1, 1.1);
Use the default UINavigationcontroller strategy.
Add a navigation controller and its view controllers.
Give both view controller navigation title. self.navigationItem.title in view did load.
Just pushing the navigation controller will default have a back button and the title of the previous view controller to it with same system stle.
Do not set any custom leftbarbutton item as this may overide the UINAvigationcontroller to show the default back button.
Hope this help.
I have made a standard Master-Detail application (with the Xcode template). I modified it, and now when I click one cell of the first table view the app shows another table view (which is the detail because is filtered by the clicked cell value).
In a standard table view I would have this situation on the UINavigationBar:
Edit Table title + (for adding new items)
I need the default back button of UINavigationBar too. Is it possible and allowed to do this? Graphically it would be like this:
< Back Table title Edit +
Do you know any other layout to display 3 button (back - edit - add) plus title all in the top bar?
Sorry for not posting images, Thanks in advance.
P.S. I'd like to know if it is possible to do the things I'm asking with built-in back button and edit button (I mean those that the system puts automatically to the navigation bar).
you need to setup array of right bar items to set up bar buttons right side...
Here is the code of 2 custom button to setup bar button items right side...you can use default button instead of this
UIButton *btnabout = [UIButton buttonWithType:UIButtonTypeCustom];
btnabout.frame = CGRectMake(0,8,30,30);
[btnabout setBackgroundImage:[UIImage imageNamed:#"about.png"] forState:UIControlStateNormal];
[btnabout addTarget:self action:#selector(callselector) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *about = [[UIBarButtonItem alloc] initWithCustomView:btnabout];
about.tintColor = [UIColor whiteColor];
UIButton *btnsetting = [UIButton buttonWithType:UIButtonTypeCustom];
btnsetting.frame = CGRectMake(0,8,30,30);
[btnsetting setBackgroundImage:[UIImage imageNamed:#"setting"] forState:UIControlStateNormal];
[btnsetting addTarget:self action:#selector(SettingData) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *setting = [[UIBarButtonItem alloc] initWithCustomView:btnsetting];
setting.tintColor = [UIColor whiteColor];
self.navigationItem.rightBarButtonItems =[NSArray arrayWithObjects:setting, about, nil];
// --- or if you want left side ----
// self.navigationItem.leftBarButtonItems =[NSArray arrayWithObjects:setting, about, nil];
[self.navigationItem setRightBarButtonItems:#[/*array of UIBarButtonItem*/] animated:NO]
Apple doc
to someone stuck with this problem in swift, here is the correct answer :
let barButton_array: [UIBarButtonItem] = [Button1, Button2]
navigationItem.setRightBarButtonItems(barButton_array, animated: false)
Controller 1 displays an array of UIImages, Controller 2 manages the paging of those images once selected, and Controller 3 just displays the images for the pages. When I tap on a cell and load a photo into the photoViewController, the paging of images works fine, but my custom navigation bar is overridden with the default bar and I am unable to add any custom buttons to it or change any of its properties in code. The only thing I found that I am able to do is hide it. If I try to change the title of the back button or add my own title to the navigation bar nothing changes. Please help with anything you can.
i think you should try like this
self.navigationItem.leftBarButtonItem = [self leftMenuBarButtonItem];
and function could be
- (UIBarButtonItem *)leftMenuBarButtonItem {
UIButton *backButton = [[UIButton alloc] initWithFrame:CGRectMake(50, 50, 50, 30)]; [backButton setImage:[UIImage imageNamed:#"bar_btn_bg.png"] forState:UIControlStateNormal];
[backButton addTarget:self action:#selector(leftDrawerButtonPress:) forControlEvents:UIControlEventTouchUpInside];
return [[UIBarButtonItem alloc] initWithCustomView:backButton];
}
if you fall in between let me know
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.