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];
Related
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.
I have a navigation controller application, and I need to set the custom action for the navigation back bar button. Tried some workarounds and not yet to find a solution.
Tried
UIBarButtonItem *backBarItem = self.navigationItem.leftBarButtonItem;
backBarItem.target = self;
backBarItem.action = #selector(popToHomeViewController);
Result : No effect. Back button pops to just previous viewController in navigation stack
UIBarButtonItem *customBarItem = [[UIBarButtonItem alloc] initWithTitle:backBarItem.title style:backBarItem.style target:self action:#selector(popViewController)];
self.navigationItem.leftBarButtonItem = customBarItem;
Result : No effect. Back button pops to just previous viewController in navigation stack
UIBarButtonItem *customBarItem = [[UIBarButtonItem alloc] initWithTitle:#"back" style:backBarItem.style target:self action:#selector(popViewController)];
self.navigationItem.leftBarButtonItem = customBarItem;
Result:Now my selector got invoked perfectly and navigated to desired viewController. Here the issue is that the back button not as like as the native back button. It is not having the bold "<" character as I have not mentioned it. If added < character it needs to be changed for ios 6 compatibility.
Any better solution to ensure ios 6 and ios 7 compatible navigation back button with custom selector?
Try this simple example will help you..
- (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];
}
Make sure you have an button image with the size of a navigation bar back button in your resource folder with name back.png
Feel free if any other assistance is required.
Happy Coding!!!!!
Try this
viewController.navigationItem.hidesBackButton = YES;
//set custom image to button if needed
UIImage *backButtonImage = [UIImage imageNamed:#"back"];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setImage:backButtonImage forState:UIControlStateNormal];
button.frame = CGRectMake(0, 0, backButtonImage.size.width, backButtonImage.size.height);
[button addTarget:viewController action:#selector(back) forControlEvents:UIControlEventTouchUpInside];
UIView *backButtonView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, backButtonImage.size.width, backButtonImage.size.height)];
[backButtonView addSubview:button];
UIBarButtonItem *customBarItem = [[UIBarButtonItem alloc] initWithCustomView:backButtonView];
viewController.navigationItem.leftBarButtonItem = customBarItem;
and in back method you can customise
- (void)back {
//Your code
}
I am trying to make my uinavigationbar to align the same way as in the iOS6 version of my app. What I am finding is that on the iOS7 the uinavigationbar items are pulled in a bit towards the title. Also for example if you are hitting just a litle right of the uinavigationitem.rightButton, the hits are not registered, however if you hit left of it for atleast 20px you can get the hits. Can anyone help me with moving that touch area to the right as well?
Here is the code that I used to setup the button:
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 44)];
UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeCustom];
rightButton.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
rightButton.imageEdgeInsets = UIEdgeInsetsMake(0, 5, 0, 0);
rightButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
rightButton.frame = view.bounds;
rightButton.bounds = CGRectMake(0,0,44,44);
[view addSubview:rightButton];
[rightButton setImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal];
[rightButton setImage:[[UIImage imageNamed:imageName] imageWithOpacity:0.8] forState:UIControlStateHighlighted];
[rightButton addTarget:target action:sel forControlEvents:UIControlEventTouchUpInside];
navItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:view];
Thanks,
Rajan
Use below code
UIImage *faceImage1= [UIImage imageNamed:#"slideshowarrow"];
UIButton *face1 = [UIButton buttonWithType:UIButtonTypeCustom];
face1.bounds = CGRectMake( 10, 0, faceImage1.size.width, faceImage1.size.height );
[face1 addTarget:self action:#selector(Goto_nextView) forControlEvents:UIControlEventTouchUpInside];
[face1 setImage:faceImage1 forState:UIControlStateNormal];
UIBarButtonItem *backButton1 = [[UIBarButtonItem alloc] initWithCustomView:face1];
self.navigationItem.rightBarButtonItem = backButton1;
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...
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:)