Custom image on uinvigation bar without borders - objective-c

The way the barbutton looks in first view
I am trying to create custom barbutton items as shown with out borders. I made it, but in the next view it is having a different rightbarbutton item, and there the previous rightbarbutton item is overlapping. Can anyone please help me.
the way the barbutton of first view overlapping the barbutton item of next view
The code goes like this:
` UIButton *refreshButton = [[UIButton alloc] initWithFrame:CGRectMake(285, 9, 27, 27)];
[refreshButton setImage:[UIImage imageNamed:#"Refresh_Icon.png"] forState:UIControlStateNormal];
[refreshButton addTarget:self action:#selector(refresh:) forControlEvents:UIControlEventTouchUpInside];
[self.navigationController.navigationBar addSubview:refreshButton];`

Try setting self.navigationItem.rightBarButtonItem = NO; in VC's viewDidLoad method.

Related

Common UIButton or UIbarbutton to navigationbar

How can we add common UIButton or UIBarButton or any UI object so that it appears in all navigationbars in the application.
The below code will work perfectly;
UIImage* image3 = [UIImage imageNamed:#"info.png"];
CGRect frameimg = CGRectMake(0, 0, image3.size.width*4, image3.size.height);
UIButton *someButton = [[UIButton alloc] initWithFrame:frameimg];
//[someButton setBackgroundImage:image3 forState:UIControlStateNormal];
[someButton setTitle:#"Category" forState:UIControlStateNormal];
[someButton addTarget:self action:#selector(popUpPicker:)
forControlEvents:UIControlEventTouchUpInside];
[someButton setShowsTouchWhenHighlighted:YES];
UIBarButtonItem *mailbutton =[[UIBarButtonItem alloc] initWithCustomView:someButton];
self.navigationItem.rightBarButtonItem=mailbutton;
self.navigationItem.leftBarButtonItem = nil;
For these, you have to use category and then you have to call it in your every view controller to have the one code for every navigationabr button action.
There is no magic formula. One way or another, you have to add the same "UIButton or UIBarButton or any UI object" to the navigationItem of every UIViewController whose view will appear under this navigation controller's navigation bar.
You can reduce the amount of repeated code by creating this object in a single place, but the act of saying
self.navigationItem.rightBarButtonItem = thisItem
or whatever it is you want to do with the bar button item, will have to be performed separately and explicitly for every view controller.

Navigation bar button unable to go back to the previous view

I have followed the informations that i have found online and I have managed to show the button but when i clicked on it, nothing happens. I am new to iOS programming and I am not sure if I did anything wrong.
UIImage *backImage = [UIImage imageNamed:#"backIcon.PNG"];
// create the button and assign the image to the button
UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
[backButton setImage:backImage forState:UIControlStateNormal];
backButton.frame = CGRectMake(0, 0, backImage.size.width, backImage.size.height);
[backButton addTarget:self action:#selector(goBack:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *customBarButton = [[UIBarButtonItem alloc] initWithCustomView:backButton];
self.navigationItem.hidesBackButton = YES;
self.navigationItem.leftBarButtonItem = customBarButton;
and this code for the button
- (void)goBack:(id)sender {
[self.navigationController popViewControllerAnimated:YES]; }
Thanks
First you don't have to hide the back button since you are adding your own, second try to replace your button target with the below:
[backButton addTarget:self.navigationController action:#selector(popViewControllerAnimated:) forControlEvents:UIControlEventTouchUpInside];
Edit:
The right way to use navigationController in storyboard will be as following:
1- Add a UInavigationController.
2- Add rootViewController relation to first VC1.
3- Add push segue from VC1 to VC2.
By this you can push and pop your VC2 properly.
Please consider to read this tutorial for better understanding storyboard, segues and navigation.

How do I make UIBarButton Work as Switch Given that UIBarButton Doesn't have Selected Property

With normal button you can do
btn.selected = !btn.selected.
How would I pull that out with UIBarButton?
You can create UIBarButtonItem with the custom view
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setImage:normalImage forState:UIControlStateNormal];
[button setImage:selectedImage forState:UIControlStateSelected];
[button addTarget:self action:#selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithCustomView:button];
Then
-(void)btnClick:(id)sender{
UIBarButtonItem *item = (UIBarButtonItem *)sender;
UIButton *button = (UIButton *)item.customView;
[button setIsSelected:YES];
}
You can use a custom Button inside UIBarItem, remain one just do with you button.
UIBarButtonItem *barButton = [[UIBarButtonItem alloc] initWithCustomView:self.yourButton]
[self.navigationItem setRightBarButtonItem:barButton];
Then:
self.yourButton.select = !self.yourButton.select;
Idea 1. If you are creating your interface in IB, try dragging a standard UIButton object into the UIToolbar. This should encapsulate the UIButton inside a UIBarButton.
Set the UIBarButton object to be plain.
Create outlets to everything as usual.
Then you can use the standard button along with all it's lovely standard things like selected state in the toolbar.
Idea 2. I've pretty much given up on using toolbars as the controls are inflexible as you're finding. Now I just replace instances of UIToolbar with UIViews and do the small amount if code it needs to manage rotation, resizing etc... I've found it more flexible and let's me includes other controls more easily.
It is not possible to have the selected property through UIBarButtonItem straight away. But you can achieve this by placing the UIButton and customizing it like a UIBarbutton. This link will be more useful to you...How do I programmatically get the state of UIBarButtonItems?

BarButtonItem resizing for unknown reason

I have a button that is initialized like that>
self.button = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone
target:self
action:#selector(doAction)];
Later, I am attaching a custom background to it, like that>
[self.button setBackgroundImage:[UIImage imageNamed:#"customNavBar_button_right_enabled"]
forState:UIControlStateNormal
barMetrics:UIBarMetricsDefault];
and puting it onto navbar.
[self.navigationItem setRightBarButtonItem:self.button
animated:NO];
This image has fixed size. I want that button to have fully custom background image with no "intelligent" resizing with UIEdgeInsets.I don't want any resizing. But for some reason, it behaves like its insets would be active and it is resized to have bigger width, with roughly the middle half of picture considerably stretched to provide for the wider button.
Why is this happening? How can I prevent this to happen?
The easiest way to achieve a bar button item with a custom image is by:
Drag a UIButton onto the storyboard.
Set the button Type to Custom and set your image for that button.
Drag the button onto your Toolbar.
The storyboard automatically creates a UIBarButtonItem and adds your UIButton as a subview. Or if you do NOT want to use storyboards you could implement the following:
// Initialize the UIButton
UIImage *buttonImage = [UIImage imageNamed:#"buttonImage.png"];
UIButton *aButton = [UIButton buttonWithType:UIButtonTypeCustom];
[aButton setImage:buttonImage forState:UIControlStateNormal];
aButton.frame = CGRectMake(0.0, 0.0, buttonImage.size.width, buttonImage.size.height);
// Initialize the UIBarButtonItem
UIBarButtonItem aBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:aButton];
// Set the Target and Action for aButton
[aButton addTarget:self action:#selector(aButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
Hope it helps!
EDIT: Do not forget to add the aBarButtonItem to the UIToolbar.

customize UIBarButtonItem to have no border?

I'm trying to create a UIBarButtonItem that looks like the "list" button on the iPod.app
so I have 2 questions:
How can I set the icon to look like the iPod's one? I saw it in other apps, but couldn't find it on xcode. (the icon that appears on the right side of the navigation bar on the "now playing" screen)
how can I set the UIBarButtonItem's image to show without it's default border?
Try adding custom view with button you want as a subview to your bar;
UIView* myView = [[UIView alloc] initWithFrame:myBar.bounds];
UIbutton* myBtn = [[UIButton alloc] initWithFrame:CGRectMake(x,y,width,height)];
[myBtn setImage:myImage forState:UIControlStateNormal];
[myView addSubview:myBtn];
[myBar addSubview:myView];