adding subviews in UINavigationBar with buttons doesn't work - iphone

I have a UINavigationController subclass and in the viewDidLoad I had the following code:
self.containerView_ = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, self.navigationController.navigationBar.frameHeight)];
[self.containerView_ setBackgroundColor:[UIColor yellowColor]];
[self.containerView_ setAutoresizingMask:UIViewAutoresizingNone];
self.profileButton_ = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 30, 30)];
[self.profileButton_ addTarget:self action:#selector(showProfileView:) forControlEvents:UIControlEventTouchUpInside];
[self.profileButton_ setCenterY:self.navigationController.navigationBar.frameHeight/2];
[self.profileButton_ setImage:[UIImage imageNamed:#"profile_icon.png"] forState:UIControlStateNormal];
self.socialButton_ = [[UIButton alloc] initWithFrame:CGRectMake(self.profileButton_.frameRight + 10, 0, 30, 30)];
[self.socialButton_ addTarget:self action:#selector(showSocialView:) forControlEvents:UIControlEventTouchUpInside];
[self.socialButton_ setCenterY:self.profileButton_.centerY];
[self.socialButton_ setImage:[UIImage imageNamed:#"social_icon.png"] forState:UIControlStateNormal];
self.notesButton_ = [[UIButton alloc] initWithFrame:CGRectMake(self.socialButton_.frameRight + 10, 0, 30, 30)];
[self.notesButton_ addTarget:self action:#selector(showNotesView:) forControlEvents:UIControlEventTouchUpInside];
[self.notesButton_ setCenterY:self.profileButton_.centerY];
[self.notesButton_ setImage:[UIImage imageNamed:#"notes_icon.png"] forState:UIControlStateNormal];
[self.containerView_ addSubview:self.profileButton_];
[self.containerView_ addSubview:self.notesButton_];
[self.containerView_ addSubview:self.socialButton_];
[self.navigationBar addSubview:self.containerView_];
I can see the UIButton but the container background is not even yellow color. When I press the button, the action of the UIButton is not called. Any idea why this is?

You are subclassing UINavigationController?
To quote Apple: "This class is not intended for subclassing."
The usual way to tweak the UINavController is to access the navBar from the perspecitve of a contained UIViewController. You would put the target/action methods in the UIViewController.

Related

How to add two buttons to the UINavigation bar title view

can any body help me how to add two buttons to the navigation title view by using the title view property. I tried but, able to add only one button by using the following code.
UIView *customView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
customView.backgroundColor = [UIColor redColor];
UIButton *b1 = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 50, 50)];
UIButton *b2 = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 50, 50)];
[b1 setTitle:#"Hai" forState:UIControlStateNormal];
[b2 setTitle:#"Hello" forState:UIControlStateNormal];
[customView insertSubview:b1 atIndex:0];
[customView insertSubview:b2 atIndex:1];
self.navigationItem.titleView = customView;
use this code:
UIView *customView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 44)];
customView.backgroundColor = [UIColor redColor];
UIButton *b1 = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 50, 50)];
UIButton *b2 = [[UIButton alloc] initWithFrame:CGRectMake(50, 0, 50, 50)];
[b1 setTitle:#"Hai" forState:UIControlStateNormal];
[b2 setTitle:#"Hello" forState:UIControlStateNormal];
[customView insertSubview:b1 atIndex:0];
[customView insertSubview:b2 atIndex:1];
self.navigationItem.titleView = customView;
Outputs:
It's because you have added the same frame for both Buttons. So, the Buttons are actually overlapped. You should use this code instead :
UIButton *b1=[[UIButton alloc]initWithFrame:CGRectMake(0, 0, 50, 50)];
UIButton *b2=[[UIButton alloc]initWithFrame:CGRectMake(60, 0, 50, 50)];
Then you can simply add these buttons to your custom view.
[cusotmView addSubView:b1];
[cusotmView addSubView:b2];
Reason for Error :
You can't directly add UIButtons. You need to wrap them as UIBarButtonItems first.
Sample Code :
UIBarButtonItem *btn1 = [[UIBarButtonItem alloc] initWithCustomView:b1];
UIBarButtonItem *btn2 = [[UIBarButtonItem alloc] initWithCustomView:b2];
[cusotmView addSubView:btn1];
[cusotmView addSubView:btn2];

Back button on custom navigation bar

I am trying to develop an app with a navigation bar of custom size. (100px) In the navigation bar, I also want to include a back button. I have added both of them using the child view, but the back button is not working:
UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
UIImage *backBtnImage = [UIImage imageNamed:#"menueButton.png"];
UIImage *backBtnImagePressed = [UIImage imageNamed:#"menueButton.png"];
backBtn.exclusiveTouch = YES;
[backBtn addTarget:self action:#selector(back:) forControlEvents:UIControlEventTouchUpInside];
[backBtn setBackgroundImage:backBtnImage forState:UIControlStateNormal];
[backBtn setBackgroundImage:backBtnImagePressed forState:UIControlStateHighlighted];
backBtn.frame = CGRectMake(0, -40, 35, 32);
UIView *backButtonView = [[UIView alloc] initWithFrame:CGRectMake(0, -40, 35 , 32)];
[backButtonView addSubview:backBtn];
UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithCustomView:backButtonView];
self.navigationItem.leftBarButtonItem = backButton;
My navigation bar size is 100 pixels tall, and when I use button position (0,0,35,32) it will work correctly. But in this case, the button is being displayed much lower than intended. What am I doing wrong?
self.btnBack = [UIButton buttonWithType:UIButtonTypeCustom];
[self.btnBack setImage:[UIImage imageNamed:#"back_active.png"] forState:UIControlStateNormal];
[self.btnBack addTarget:self action:#selector(back) forControlEvents:UIControlEventTouchUpInside];
self.btnBack.frame = CGRectMake(5, 9, 50, 30);
[self.navigationController.navigationBar addSubview:self.btnBack];
check this link also.
Update:for back button
-(void)back
{
[self.btnBack removeFromSuperview];
[self.navigationController popViewControllerAnimated animated:NO];
}
Try allocing backButtonView like this
UIView *backButtonView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 35 , 32)];
Change : orgin of view to (0,0)
Edit
Try
backBtn.frame = CGRectMake(0, 0, 35, 32);
UIView *backButtonView = [[UIView alloc] initWithFrame:CGRectMake(0, -40, 35 , 32)];

How to add a custom image to a navigation bar and keep the back method in tact?

I have the following to add a custom image as a back button. The problem is that it overrides the default navigation controller back method.
How can I correct this?
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setImage:[UIImage imageNamed:#"button-back-arrow.png"] forState:UIControlStateNormal];
//[button addTarget:self action:#selector(favouriteButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
[button setFrame:CGRectMake(280, 25, 40, 29)];
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button];
thanks for any help
just add this line and method..
[button addTarget:self
action:#selector(BtnBack_Clicked:)
forControlEvents:UIControlEventTouchDown];
and call this method
-(IBAction)BtnBack_Clicked:(id)sender{
[self.navigationController popViewControllerAnimated:YES];
}
UIView *btnView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 84, 31)];
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
btn.frame = CGRectMake(0, 0, 84, 31);
[btn setBackgroundImage:[UIImage imageNamed:#"yourImage.png"] forState:UIControlStateNormal];
[btn addTarget:self action:#selector(action) forControlEvents:UIControlEventTouchUpInside];
[btnView addSubview:btn];
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:btnView];
[btnView release];

Place custom view on Navigation Bar

I want to display Image,Title name,and two left right buttons. That I have done successfully.
Here is the code:
UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 200, 100)];
UIButton *leftButton = [[UIButton alloc]init];
[leftButton setImage:[UIImage imageNamed:#"previous-arrow.png"] forState:UIControlStateNormal];
[leftButton addTarget:self action:#selector(goPrevious:) forControlEvents:UIControlEventTouchUpInside];
[leftButton setFrame:CGRectMake(20, 50, 14, 13)];
UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"Title.png"]];
[imageView setFrame:CGRectMake(leftButton.frame.origin.x+leftButton.frame.size.width+4, 20, 50, 50)];
UILabel *nameLabel = [[UILabel alloc]initWithFrame:CGRectMake(imageView.frame.origin.x+imageView.frame.size.width+4, 30, 100, 40)];
NSString *cName = [NSString stringWithFormat:#"%#",[NameArray objectAtIndex:indexPath.section]];
[nameLabel setTextColor:[UIColor whiteColor]];
[nameLabel setBackgroundColor:[UIColor clearColor]];
[nameLabel setContentMode:UIViewContentModeScaleAspectFill];
[nameLabel setTextAlignment:UITextAlignmentLeft];
CGSize expectedLabelSize = [cName sizeWithFont:nameLabel.font
constrainedToSize:CGSizeMake(100, 40)
lineBreakMode:nameLabel.lineBreakMode];
[nameLabel setFrame:CGRectMake(imageView.frame.origin.x+imageView.frame.size.width+4, 35, expectedLabelSize.width, expectedLabelSize.height)];
nameLabel.text=cName;
UIButton *rightButton = [[UIButton alloc]init];
[rightButton setImage:[UIImage imageNamed:#"next-arrow.png"] forState:UIControlStateNormal];
[rightButton setFrame:CGRectMake(nameLabel.frame.origin.x+nameLabel.frame.size.width+4, 50, 14, 13)];
[rightButton addTarget:self action:#selector(next:) forControlEvents:UIControlEventTouchUpInside];
[view addSubview:nameLabel];
[view addSubview:imageView];
[view addSubview:leftButton];
[view addSubview:rightButton];
self.navigationItem.titleView = view;
This code is written in cellForRowAtIndexPath. The view is showing properly on navigation bar but after scrolling the tableview. Initially it is displaying on wrong coordinates.
Please Suggest.
Put this code in viewWillAppear or viewDidAppear. It's not meant to be in tableView protocol!

Changing position of button label in Xcode

I'm just wondering if anyone could help me change the position of a button label for a button that I've made with code.
Thanks a lot,
Chris
If you mean UIButton, look at UIButton's titleEdgeInsets property.
So the button label is an instance of UILabel, isn't it ?? Set the position of that label by setting the frame to it with respect to the UIButton frame.
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button setFrame:CGRectMake(3, 20, w, h)];
UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 20, w, h)];
[titleLabel setText:#"TITLE"];
[button addSubview:titleLabel];
[self.view addSubview:button];
[titleLabel release];
I hard coded the x and y positions of the button as well as for the label. You set some other value's and place the label in the respective position where ever you want.
If you want to set only the text position of that label you can do it like this:
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button setFrame:CGRectMake(3, 20, w, h)];
UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, w, h)];
[titleLabel setText:#"TITLE"];
[titleLabel setTextAlignment:UITextAlignmentCenter];//(UITextAlignmentRight/UITextAlignmentLeft)
[button addSubview:titleLabel];
[self.view addSubview:button];
[titleLabel release];
Hope it will help you.
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(0,0,50,32);
[button setBackgroundImage:[UIImage imageNamed:#"btn_back.png"] forState:UIControlStateNormal];
[button addTarget:self action:#selector(BackBtnClicked) forControlEvents:UIControlEventTouchUpInside];
UIView *backButtonView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 50, 32)];
backButtonView.bounds = CGRectOffset(backButtonView.bounds, -1, -4);
[backButtonView addSubview:button];
UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backButtonView];
//barButtonItem.imageInsets = UIEdgeInsetsMake(-6, 20, 30, 0);
[self.navigationItem setLeftBarButtonItem:barButtonItem];