Can't add rightBarButton to UINavigationBar - ios

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...

Related

iOS - make UIImage take the same size as UIButton

I create a custom bar button using
+ (UIBarButtonItem *)barBtnItem:(id)target action:(SEL)action imageName:(NSString *)imageName frame:(CGRect)frame
{
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
[btn setImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal];
[btn addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
[btn setFrame:frame];
UIBarButtonItem *barBtn = [[UIBarButtonItem alloc] initWithCustomView:btn];
return barBtn;
}
The frame is of size
CGRectMake(0, 0, 22, 22)
However, I think due to aspect ratio, my image gets smaller but is correctly centered in the UIButton
However, I want the image to become the same size as of UIButton.
Can I do that?
I used XCode 7 and tried its slicing feature. But I could not make it work.
If I could do it via code?
I also looked at UIEdgeinsets but I do not fully understand cap/insets concepts.
Set the content mode of the button to UIViewContentModeScaleToFill.
btn.imageView.contentMode = UIViewContentModeScaleToFill
btn.imageView.frame = btn.bounds
check after write below code :
btn.contentMode= UIViewContentModeScaleToFill;
btn.contentEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 0);
btn.imageEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 0);
Still not work then try below line of code. Set background image of UIButton instead of image.
+ (UIBarButtonItem *)barBtnItem:(id)target action:(SEL)action imageName:(NSString *)imageName frame:(CGRect)frame
{
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
[btn setBackgroundImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal];
[btn addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
[btn setFrame:frame];
UIBarButtonItem *barBtn = [[UIBarButtonItem alloc] initWithCustomView:btn];
return barBtn;
}
if not work use below line of code :
UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:imageName] style:UIBarButtonItemStylePlain target:self action:#selector(handleBack:)];
you can do it through AutoLayouts !!! set image Constraints from top , bottom , left , right equal to Button !!

How to cancel UIBarbuttonItem setTitle shining effect

Like image shows, I used UIBarbuttonItem as rightBarButtonItem, what I want to do is just change rightBarButton's title when I select a picture or deselect a picture, so I used code like this:
[self.rightBarButton setTitle:[NSString stringWithFormat:#"完成(%#/%#)",#(_selectedAssets.count),#(self.maxPicturesNum)]];
but the barbutton will show shin when its text changed, I try to use UIButton instead of UIBarButtonItem, and shin effect disappear indeed, but UIButton will be close to right bounds far, can you help me?
You can create UIButton and assign it to your rightBarButton
UIButton *rightButton = [[UIButton alloc] initWithFrame:someFrame];
[rightButton setBackgroundImage:someImage forState:UIControlStateNormal];
[rightButton addTarget:self action:#selector(back:) forControlEvents:UIControlEventTouchUpInside];
rightButton.titleLabel.text = [NSString stringWithFormat:#"完成(%#/%#)",#(_selectedAssets.count),#(self.maxPicturesNum)];
rightButton.titleLabel.font = someFont;
UIBarButtonItem *rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:rightButton];
self.rightBarButton = rightBarButtonItem;
And while updating label use rightButton.titleLabel.text
For Adjusting label use this:
- (void)setTitlePositionAdjustment:(UIOffset)adjustment
forBarMetrics:(UIBarMetrics)barMetrics
Finally,I do like this:
- (void)loadRightButtonItem{
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(30, 0, 120, 40);
button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
[button setTitle:#"完成(0/9)" forState:UIControlStateNormal];
button.tintColor = [UIColor whiteColor];
[button setTitleShadowColor:[UIColor whiteColor] forState:UIControlStateHighlighted];
// [button setTitleColor:[self.buttonFolder titleColorForState:UIControlStateHighlighted] forState:UIControlStateHighlighted];
[button addTarget:self action:#selector(onFinishClicked:) forControlEvents:UIControlEventTouchUpInside];
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 135, 40)];
UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithCustomView:view];
[view addSubview:button];
self.navigationItem.rightBarButtonItem = item;
_rightBarButton = button;
}
It cancel the shin effect.
thanks to #RoHaN,your idea did great inspired me.

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];

Customize navigation bar button in iOS6

Update - One Possible Solution
Based on answer given by Alexander Merchi in this post (UIBarButtonItem Custom view in UINavigationBar), a solution is found. But still don't know how to change the position of the image properly.
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
[btn setFrame:CGRectMake(0, 0, 28.0f, 28.0f)]; // 28 points is the width or height of the button image
[btn setBackgroundImage:[UIImage imageNamed:#"button.png"] forState:UIControlStateNormal];
[btn addTarget:self action:#selector(onClickMenuButton) forControlEvents:UIControlEventTouchUpInside];
btnMenu = [[UIBarButtonItem alloc] initWithCustomView:btn];
Original Post
In iOS 6, the goal is like this:
while current result is like this:
My code is
btnMenu = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:#"button.png"]
style:UIBarButtonItemStylePlain
target:self
action:#selector(onClickMenuButton)];
button.png is this the white circle with white three bars inside and a tranparent background.
UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeCustom];
rightButton.frame = CGRectMake(0, 0, 30, 30);
[rightButton setImage:[UIImage imageNamed:#"Button-normal"] forState:UIControlStateNormal];
[rightButton setImage:[UIImage imageNamed:#"logout-hover"] forState:UIControlStateHighlighted];
[rightButton addTarget:self action:#selector(logOut) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:rightButton];
self.navigationItem.leftBarButtonItem = rightBarButtonItem;
I am accomplishing it by
UIImageView *uView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"btn_back_ontouch_scr11.png"]];
UIButton *leftButton = [UIButton buttonWithType:UIButtonTypeCustom];
leftButton.bounds = CGRectMake( 0, 0, uView.frame.size.width, uView.frame.size.height );
[leftButton setImage:uView.image forState:UIControlStateHighlighted];
[leftButton setImage:[UIImage imageNamed:#"btn_back_normal_scr11.png"]forState:UIControlStateNormal];
[leftButton addTarget:self.navigationController action:#selector(popViewControllerAnimated:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *aButton = [[UIBarButtonItem alloc] initWithCustomView:leftButton];
self.scrollView.indicatorStyle=UIScrollViewIndicatorStyleWhite;
self.navigationItem.leftBarButtonItem = aButton;
self.navigationItem.hidesBackButton = YES;
This should work.

Custom logo and button not appear in UINavigationBar

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:)

Resources