I'm customizing UINavigationBar and have to set a custom background image to the navigation buttons. This is done this way:
for (UIView *view in self.navigationController.navigationBar.subviews) {
if ([[[view class] description] isEqualToString:#"UINavigationButton"]) {
UINavigationButton *button = {(UINavigationButton *)view};
[button setBackgroundImage:[UIImage imageNamed:#"bar_button.png"] forState:UIControlStateNormal];
[button setBackgroundImage:[UIImage imageNamed:#"bar_button_black.png"] forState:UIControlStateSelected];
[button setBackgroundImage:[UIImage imageNamed:#"bar_button_black.png"] forState:UIControlStateHighlighted];
}
}
Won't this be rejected by Apple?
How can I make UIButton/UIView corners to be rounded?
A UIView object has a property named layer (CALayer) and a layer has a cornerRadius property.
Create your custom buttons as normal UIButtons, with your custom image. Then create UIBarButtonItems with your new UIButton as a custom view (you can see how to do this in the UIBarButtonItem class reference).
Then, set your view controller's navigationItem to have your new UIBarButtonItems as the leftButtonItem, rightButtonItem and so on.
Instead of this do like this.Take a UIButton and then add it to the navigation buttons.
Related
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.
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.
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?
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.
How can I load a jpg image into the UIButton.
I want one image for when it is Not pressed.
And another image for when it is pressed.
thanks
(looking to do this programmatically... no interface builder)
[myButton setBackgroundImage:[UIImage imageNamed:#"not_pressed.jpg"] forState:UIControlStateNormal];
[myButton setBackgroundImage:[UIImage imageNamed:#"pressed.jpg"] forState:UIControlStateHighlighted];
not_pressed.jpg and pressed.jpg are needed to be present in resource. Also you can use setImage:forState:. Please check UIButton reference for details of these methods.
You can see in interface builder there are properties and you can see that there are options for backgroundImage and Image for each state(normal,highlighted,selected,disabled) go to interface builder properties of UIButton you will find there. For images to appear in the combo box you need to add images to your project.
UPDATE
Code Snippet
UIButton * btnShare=[[UIButton alloc] initWithFrame:CGRectMake(0,0,50,50)];
[btnShare setImage:[UIImage imageNamed:#"checkbox.png"] forState:UIControlStateNormal];
[btnShare setImage:[UIImage imageNamed:#"checkboxChecked.png"] forState:UIControlStateHighlighted];
[btnShare setImage:[UIImage imageNamed:#"checkboxChecked.png"] forState:UIControlStateSelected];
[btnShare addTarget: self action: #selector(accessoryButtonTapped:withEvent:)
forControlEvents: UIControlEventTouchUpInside];