Customize Navigation Bar in ios - ios

I used Navigation Controller & navigation Bar in StroryBoard.
I want to customize navigation Bar for one view as follow for right side of navigation bar.
My tried code
UIButton *btn_list=[UIButton buttonWithType:UIButtonTypeCustom];
btn_list.frame=CGRectMake(0, 0, 60, 30);
[btn_list setTitle:#"Liste" forState:UIControlStateNormal];
[btn_list setBackgroundImage:[UIImage imageNamed:#"red-left.png"] forState:UIControlStateNormal];
[btn_list setBackgroundImage:[UIImage imageNamed:#"black-left.png"] forState:UIControlStateSelected];
UIBarButtonItem *list_bar=[[UIBarButtonItem alloc]initWithCustomView:btn_list];
UIButton *btn_map=[UIButton buttonWithType:UIButtonTypeCustom];
btn_map.frame=CGRectMake(0, 0, 60, 30);
[btn_map setTitle:#"Karte" forState:UIControlStateNormal];
[btn_map setBackgroundImage:[UIImage imageNamed:#"red-right.png"] forState:UIControlStateNormal];
[btn_list setBackgroundImage:[UIImage imageNamed:#"black-right.png"] forState:UIControlStateSelected];
UIBarButtonItem *map_bar=[[UIBarButtonItem alloc]initWithCustomView:btn_map];
self.navigationItem.rightBarButtonItems=[[NSArray alloc]initWithObjects:list_bar,map_bar, nil];
Problem of tried code: There is a space between two buttons.
How can I achieve this?

Just use Segmented Control: you have a full example HERE

declare a segment control with custom view
UISegmentedControl *control = (UISegmentedControl *) [segmentBarButton customView];
and then add it to barbuttonitem view
UIBarButtonItem *segmentBarButton=[[UIBarButtonItem alloc] initWithCustomView:control];
dont forget to write the lines that relates to segment control customized view like ur requirement adding red or black images to the segment 0 or 1.

Related

UINavigationBar barButtonItem change back button image and title

Is it possible to change the back button on a UINavigationBar and change the title?
When I try to set the customView property, I get an image right next to the default button.
I use this code
self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithImage:backButtonImage landscapeImagePhone:nil style:UIBarButtonItemStylePlain target:nil action:nil];
I want the title to be "Back" which is easy enough to do in Storyboard. But the problem is that no matter if I use code above or use customView property, the default back button remains.
You can add a UIImage to the UIButton. And, then use it as a custom back button. Here's a quick example:
// Custom image
UIImage *backButtonImage = [UIImage imageNamed:#"button-background-image.png"];
UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
[backButton setFrame:CGRectMake(0, 0, 100, 30)];
[backButton setBackgroundImage:backButtonImage forState:UIControlStateNormal];
// Custom title
[backButton setTitle:#"Back" forState:UIControlStateNormal];
[backButton setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
[backButton addTarget:self action:#selector(barPayButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
[backButton setShowsTouchWhenHighlighted:NO];
UIBarButtonItem *buttonOnBar =[[UIBarButtonItem alloc] initWithCustomView:backButton];
self.navigationItem.leftBarButtonItem = buttonOnBar;
Note: You will loose the chevron (system-provided back arrow) which was introduced in iOS7. It goes as a title and chevron together presenting your previous view controller.
Update:
You can also use UIEdgeInsets to resize your image intelligently.
UIEdgeInsets edgeInsets = UIEdgeInsetsMake(0, 0, 15, 10);
UIImage *backButtonImage = [[UIImage imageNamed:#"button-background-image.png"] resizableImageWithCapInsets:edgeInsets];
You can achieve what you want by setting the navigationItem.leftBarButtonItem of the view controller that's being pushed to the custom bar button item with the look you want. Good Luck!

IOS 8 - Navigation bar buttons issue

My app has simple viewControll(XIB, no storyboard) with 6 sub views
When user click on buttons sub view bring in front of current view (by using call [self.view bringSubviewToFront:usageView];)
App working fine on IOS 6 and 7 but I facing issue on IOS 8.
When I click on buttons for bring sub view in front sub view come up with like below image
Navigation bar right side buttons "Done" show improper, its hide only in iOS 8
Please help me for this issue I am new in IOS development. If your required any more information please comment.
Add this in viewDidLoad method of this viewController and customise it like you want
UIImage *backButtonImage = [UIImage imageNamed:#"Back_A.png"];
UIImage *backButtonImage1 = [UIImage imageNamed:#"Back_B.png"];
UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
[backButton setImage:backButtonImage
forState:UIControlStateNormal];
[backButton setImage:backButtonImage1
forState:UIControlStateSelected];
[backButton setImage:backButtonImage1
forState:UIControlStateHighlighted];
backButton.frame = CGRectMake(0, 0, 55, 45);
[backButton addTarget:self
action:#selector(goBack)
forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *backBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
self.navigationItem.rightBarButtonItem = backBarButtonItem;
With frame you can show the button where you want.

UIButton has dot when set to selected inside UIToolbar

I'm adding a UIButton to a UIToolbar that I don't want to have as a UIBarButtonItem so that I have more control over the position of the UIButton inside the frame. I set a different title for both "Normal" and "Selected" states. Everything acts completely normal for a UIButton except when it's "Selected" there is a 1 pixel dot in the front of the UIButton's title.
toolbar:
UIToolbar *customView = [[UIToolbar alloc] initWithFrame:CGRectMake(HEADER_MARGIN, 0, HEADER_WIDTH, HEADER_HEIGHT)];
[customView setBarTintColor:[UIColor whiteColor]];
button:
UIButton *listOrderButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, ORDER_BUTTON_WIDTH, ORDER_BUTTON_HEIGHT)];
[listOrderButton setCenter:CGPointMake(customView.center.x, HEADER_HEIGHT/2)];
[listOrderButton.titleLabel setFont:[UIFont systemFontOfSize:ORDER_BUTTON_FONT_SIZE]];
[listOrderButton.titleLabel setTextAlignment:NSTextAlignmentCenter];
[listOrderButton setTitle:#"ABC" forState:UIControlStateNormal];
[listOrderButton setTitle:#"Time" forState:UIControlStateSelected];
[listOrderButton setTitleColor:[UIColor blueAppColor] forState:UIControlStateNormal];
[listOrderButton addTarget:self action:#selector(listOrderChange:) forControlEvents:UIControlEventTouchUpInside];
[customToolbar addSubview:listOrderButton];
and the selector:
- (void)listOrderChange:(UIButton *)sender {
[sender setSelected:!sender.selected];
}
Image of what's happening:
- No dot when not selected
- When selected, there is a dot
This happens both on the device and in the simulator. Changing the text color for any and all states has no bearing on the color of the dot itself.
I should also point out that the dot is two points wide and one point high.
Instead of using UIButton, I know you dont want to use a UIBarButton, but would it be possible to create a UIBarButton programmatically giving it all the features of a UIButton?
Since UITabBar is like UINavigation Bar and apple suggests using UIBarButtonItem for bar/tabBar Buttons... I believe using a UIBarButtonItem might solve the problem....

How to implement image and text for bar button item

The iPhone's default calendar program shows the "<" and text at the same time in the navigator bar.
I want to have image and text simultaneously as well.
How is this implemented in Xcode?
Create a custom button
UIButton *barBt =[[UIButton alloc] initWithFrame:CGRectMake(0, 0, 60, 44)];
[barBt setImage:[UIImage imageNamed:#"my_image.png"] forState:UIControlStateNormal];
[barBt setTitle:#"MyTitle" forState:UIControlStateNormal];
[barBt addTarget:self action: #selector(pressed:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *barItem = [[UIBarButtonItem alloc]init];
[barItem setCustomView:barBt];
self.navigationItem.leftBarButtonItem = barItem;
You can only have image or text inside the UIBarbuttonItem, the < is added by iOS by default as a back button for the navigation hierarchy.
You have to design the < button in the image itself if you want both image and the < in the barButton.

Make custom Navigation Buttons glow when touched?

In my navigation bar I have two right button bar items. One is a custom button and the other uses the info light button type. Both work great but I would like to make the custom button have the default glow the same way the info button does when you touch it. Is this possible?
// Default info light btn
UIButton *infoBtn = [UIButton buttonWithType:UIButtonTypeInfoLight];
UIBarButtonItem *infoBtnItem = [[UIBarButtonItem alloc]initWithCustomView:infoBtn];
// Custom Btn
UIButton *customButton = [UIButton buttonWithType:UIButtonTypeCustom];
[customButton setFrame:CGRectMake(0, 0, btnImage.size.width, btnImage.size.height)];
[customButton setBackgroundImage:btnImage forState:UIControlStateNormal];
[customButton addTarget:self action:#selector(rightDrawerButtonPress:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *customButtonItem = [[UIBarButtonItem alloc] initWithCustomView:wlButton];
NSArray *rightBtns = #[customButtonItem, infoBtnItem];
[navItem setRightBarButtonItems:rightBtns animated:YES];
you just need to put one more line of code like:-
customButton.showsTouchWhenHighlighted = TRUE;
After this your code Output something like this:-

Resources