Custom logo and button not appear in UINavigationBar - ios

I Have UINavigarionController and I want to add custom background and logo and left button to the navigation bar I could add background but my logo and button not appear, would you please help me , Thanks in advance!

Your code worked and i am getting the logo and button but with one change
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setImage:[UIImage imageNamed:#"menu.png"] forState:UIControlStateNormal];
//[button addTarget:self action:#selector(parametres:) forControlEvents:UIControlEventTouchUpInside];
[button setFrame:CGRectMake(0, 0, 25, 25)];
UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithCustomView:button];
self.navigationItem.rightBarButtonItem = rightButton;
UIImageView *navigationImage=[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 120, 30)];
navigationImage.image=[UIImage imageNamed:#"menu.png"];
UIImageView *workaroundImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 120, 30)];
[workaroundImageView addSubview:navigationImage];
self.navigationItem.titleView= workaroundImageView;
self.navigationItem.titleView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"menu.png"]];
Just specify the image type i.e not menu as menu.png and just add it for your navigationItem by setting UINavigationControllerDelegate to you class as above good luck and please share your results

barButton can't be set to be used in whole app.
Each ViewController provides own barButtonItems and titleView to the navigationItem so you have to move the code of setting logo and button to the rootViewController class

You cannot create in your AppDelegate
Open your ViewController.m
- (void)viewDidLoad
{
self.navigationItem.title=#"Title"; //Set the title
[self CustomButton];
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
}
-(void) CustomButton
{
UIButton *but=[UIButton buttonWithType:UIButtonTypeCustom];//create a button
but.frame=CGRectMake(290, 4, 40, 40);// set the button frame size
[but setImage:[UIImage imageNamed:#"image.png"] forState:UIControlStateNormal];
[but addTarget:self action:#selector(call here) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *btn = [[UIBarButtonItem alloc] initWithCustomView:but];
self.navigationItem.rightBarButtonItem = btn;
self.navigationController.navigationBar.barStyle = UIBarStyleDefault;
}
Hope its useful for you:)

Related

UINavigationItems Stretching in iOS11

I'm updating an app to ensure that it works as expected in iOS11. The only issue I've run into is that the navigation bar looks quite different in iOS11. The position of the elements and size is not the same between versions. I've looked at my code and i'm explicitly setting frame sizes and edge insets, why are these values are not respected in iOS11?
//-- Create Right Navigation Item Buttons
// create kabob btn
UIButton *kabobBtn = [UIButton buttonWithType:UIButtonTypeCustom];
[kabobBtn addTarget:self action:#selector(viewUsersClicked:)forControlEvents:UIControlEventTouchUpInside];
kabobBtn.clipsToBounds = YES;
[kabobBtn setImage:[UIImage imageNamed:#"KabobMenu2_0071bc_Right.png"]
forState:UIControlStateNormal];
[kabobBtn.imageView setContentMode:UIViewContentModeScaleAspectFit];
[kabobBtn setFrame:CGRectMake(0, 0, 32, 32)];
// create user profile button
UIButton *userBtn = [UIButton buttonWithType:UIButtonTypeCustom];
[userBtn setImage:[UIImage imageNamed:#"people.png"]
forState:UIControlStateNormal];
[userBtn addTarget:self action:#selector(viewUserProfileClicked:)forControlEvents:UIControlEventTouchUpInside];
[userBtn setFrame:CGRectMake(0, 0, 38, 38)];
userBtn.clipsToBounds = YES;
[userBtn.layer setCornerRadius:(38/2)];
[userBtn.layer setMasksToBounds:YES];
[userBtn.layer setBorderColor:[[UIColor blackColor]CGColor]];
[userBtn.layer setBorderWidth:1];
self.userPictureURL = [defaults objectForKey:#"userPicture"];
[userBtn setImage:[UIImage imageNamed:#"defaultUserImage.png"] forState:UIControlStateNormal];
[userBtn setContentMode:UIViewContentModeScaleAspectFill];
[userBtn setBackgroundColor:[UIColor clearColor]];
// add buttons to bar button items
UIBarButtonItem *barButton = [[UIBarButtonItem alloc] initWithCustomView:kabobBtn];
UIBarButtonItem *barButton2 = [[UIBarButtonItem alloc] initWithCustomView:userBtn];
// add bar button items to right bar button item array
self.navigationItem.rightBarButtonItems = #[barButton,barButton2];
//-- Create Left Hamburger Button
UIButton *customOpen = [UIButton buttonWithType:UIButtonTypeCustom];
[customOpen setFrame:CGRectMake(0, 0, 30, 44)];
[customOpen setImage:[UIImage imageNamed:#"hamburger.png"] forState:UIControlStateNormal];
[customOpen setImageEdgeInsets:UIEdgeInsetsMake(10, -5, 16, 10)];
UIBarButtonItem *openItem = [[UIBarButtonItem alloc] initWithCustomView:customOpen];
if(self.showHamburger){
self.navigationItem.leftBarButtonItem = openItem;
}else{
self.navigationItem.leftBarButtonItem = nil;
}
Apparently iOS 11 uses autolayout for nav bar buttons. Use auto layout constraints to set button width and height.
[userBtn.widthAnchor constraintEqualToConstant:38.f].active = YES;
[userBtn.heightAnchor constraintEqualToConstant:38.f].active = YES;
This should fix the stretching issue.
Thanks to this answer

UINavigatiobar right barbutton item image not visible

I am creating a custom right bar button item for UINavigation bar. Here is the code I am using,
UIView *container = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 40)];
UIButton *backPageButton = [UIButton buttonWithType:UIButtonTypeCustom];
[backPageButton setFrame:CGRectMake(0, 0, 50, 30)];
[ backPageButton setImage:[UIImage imageNamed:#"left_arrow.png"] forState:UIControlStateNormal];
backPageButton.imageView.contentMode = UIViewContentModeScaleAspectFill;
[backPageButton addTarget:self action:#selector(backButtonAction) forControlEvents:UIControlEventTouchUpInside];
[backPageButton setUserInteractionEnabled:YES];
[container addSubview:backPageButton];
UIButton *forwardPageButton = [UIButton buttonWithType:UIButtonTypeCustom];
[forwardPageButton setFrame:CGRectMake(30, 0, 50, 30)];
[ forwardPageButton setImage:[UIImage imageNamed:#"right_arrow.png"] forState:UIControlStateNormal];
[forwardPageButton addTarget:self action:#selector(forwardButtonAction) forControlEvents:UIControlEventTouchUpInside];
forwardPageButton.imageView.contentMode = UIViewContentModeScaleAspectFill;
[forwardPageButton setUserInteractionEnabled:YES];
[container addSubview:forwardPageButton];
UIBarButtonItem* item = [[UIBarButtonItem alloc] initWithCustomView:container];
// set the nav bar's right button item
self.navigationItem.rightBarButtonItem = item;
For some reason the button images are not visible. I added a background color to each button and can see that the buttons are actually added to navigation bar and also the click events are working fine. But button images are not visible.
How to fix it?
Thanks
Of course you've made a double check that images belong to your target?
To debug your views in runtime you could use small but very useful button in Xcode
more about this: https://developer.apple.com/library/ios/recipes/xcode_help-debugger/using_view_debugger/using_view_debugger.html

Navigation Bar custom button

backButton = [UIButton buttonWithType:UIButtonTypeCustom];
[backButton addTarget:self action:#selector(gotoAmphorasViewController) forControlEvents:UIControlEventTouchUpInside];
[backButton setFrame:CGRectMake(0.0f,0.0f, 44,44)];
The problem i am facing is that though the button dimensions are44*44, wherever i tap anywhere around it, the the button action is fired.
Please try the bellow code : its working properly
- (void)viewDidLoad {
[super viewDidLoad];
UIImage *buttonImage = [UIImage imageNamed:#"back.png"];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setImage:buttonImage forState:UIControlStateNormal];
button.frame = CGRectMake(0, 0, buttonImage.size.width, buttonImage.size.height);
[button addTarget:self action:#selector(back) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *customBarItem = [[UIBarButtonItem alloc] initWithCustomView:button];
self.navigationItem.leftBarButtonItem = customBarItem;
[customBarItem release];
}
-(void)back {
[self.navigationController popViewControllerAnimated:YES];
}
Its not a bug. It is a default behaviour. In iPhone, for navigation bar buttons the touch detection is little more expanded rather than its frame. Just have a look on any other application. Everywhere the button get fired if we tap nearer but outside its frame.
It's the intended behaviour, if you really want to limit the area of the touch, you can wrap the button inside a UIView:
UIView *buttonContainer = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 44, 44)];
[buttonContainer addSubview:button];
_barButton = [[UIBarButtonItem alloc] initWithCustomView:buttonContainer];

Can't add rightBarButton to UINavigationBar

I've subclassed UINavigationController to my pop & push needs and now I want to add a custom background image and some custom buttons to the UINavigationBar.
I've read that I can do it (since iOS 5) inside the appdelegate but I'd like to keep it inside the UINavigationController subclass for future use.
I've tried doing something like this:
-(void)viewDidLoad
{
[super viewDidLoad];
UIButton *rightbutton = [UIButton buttonWithType:UIButtonTypeCustom];
[rightbutton setFrame:CGRectMake(280, 0, 56, 39)];
[rightbutton setImage:[UIImage imageNamed:#"top.png"] forState:UIControlStateNormal];
[rightbutton addTarget:self action:#selector(popMePlease) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *rightBarButton=[[UIBarButtonItem alloc] initWithCustomView:rightbutton];
[[self navigationItem] setRightBarButtonItem:rightBarButton];
}
But I can't see the rightBarButton. I don't know if viewDidLoad is the place to put it in.
In your code button x position starts from 280 . It shoud be near to 0.
Replace following line
[rightbutton setFrame:CGRectMake(280, 0, 56, 39)];
with
[rightbutton setFrame:CGRectMake(0, 0, 56, 39)];
Try this it's working fine.
UIButton *myButton = [UIButton buttonWithType:UIButtonTypeCustom];
myButton.frame = CGRectMake(0, 0, 40, 40);
[myButton setBackgroundImage:[UIImage imageNamed:#"imTitleBG.png"] forState:UIControlStateNormal];
UIBarButtonItem *rightBtn = [[UIBarButtonItem alloc]initWithCustomView:myButton];
self.navigationItem.rightBarButtonItem = rightBtn;
try this code
UIBarButtonItem *btn = [[UIBarButtonItem alloc]
initWithCustomView:myButton];
self.navigationItem.rightBarButtonItem=btn;
-(void)Action
{
// Do what you want here
}
try this code might be helpful to you...

UIButton not showing in iOS

Following is a code to display a button to the toolbar.
UIButton myButton = [UIButton buttonWithType:UIButtonTypeCustom];
UIImage *myImage = [UIImage imageNamed:#"img1.png"];
[myButton setBackgroundImage:myImage forState:UIControlStateNormal];
UIBarButtonItem *myToolbarButton = [[UIBarButtonItem alloc] initWithCustomView:myButton];
Nothing gets displayed on the toolbar. Please tell me what is wrong.
[myToolbar setItems:[NSArray arrayWithObject:myToolbarButton]]; // might help
OR if it's a navigation bar
self.navigationItem.rightBarButtonItem = myToolbarButton;
Also you should set the frame for the custom button, something like this:
UIButton *mybutton = [[UIButton alloc] initWithFrame:CGRectMake:0,0,200,50];
Then if you want to add a selector/callback:
[myButton addTarget:self action:#selector(someMethod) forControlEvents:UIControlEventTouchUpInside];
self.navigationItem.leftBarButtonItem = myButtonItem; // and change for the right button.
Where self is a UIViewController.
You forgot to add this button to the toolbar!
NSArray *toolbarItems = [[NSArray alloc] initWithObjects:myToolbarButton,nil]; // you can more buttons here
[aToolbar setItems:toolbarItems animated:NO]; // change to YES if you want to have nice animation
[toolbarItems release];
If you want to add a button to navigation bar, use this:
[self.navigationItem setRightBarButtonItem:myToolbarButton]; // or setLeft...
Also, you should remember to set the frame of myButton:
[myButton setFrame:CGRectMake(0, 0, 30, 32)];

Resources