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];
Related
I'm using iOS 6.1 and want to add a UIBarButtonItem of custom width with title "Info" to the UINavigationItem programmatically when the view loads. I don't want to use an image to achieve this.
I read several threads about how to change the width of a UIBarButtonItem and none seem to work for me.
I tried this but the button does not appear:
- (void)viewDidLoad
{
UIView *infoButtonView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 50, 44)];
UIBarButtonItem *infoButton2 = [[UIBarButtonItem alloc] initWithCustomView:infoButtonView];
[infoButton2 setStyle:UIBarButtonItemStyleBordered];
[infoButton2 setTitle:#"Info"];
self.navigationItem.rightBarButtonItem = infoButton2;
}
What's wrong with this code?
//This worked for me
UIButton *searchButton =[[UIButton alloc]init];
searchButton.frame=CGRectMake(0, 0, 200, 40);
[searchButton setTitle:#"Info" forState:UIControlStateNormal];
searchButton.backgroundColor= [UIColor greenColor];
[searchButton addTarget:self action:#selector(onFilter) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *searchBarButton=[[UIBarButtonItem alloc] initWithCustomView:searchButton] ;
self.navigationItem.rightBarButtonItem = searchBarButton;
you can also add button just like this..
- (void)viewDidLoad
{
UIView *infoButtonView = [[UIView alloc] initWithFrame:CGRectMake(0, 20, 50, 44)];
[infoButtonView setBackgroundColor:[UIColor clearColor]];
UIButton *button1 = [[UIButton alloc]initWithFrame:CGRectMake(0,15,45,21)];
[button1 setTitle:#"Info" forState:UIControlStateNormal];
button1.backgroundColor=[UIColor redColor];
[infoButtonView addSubview:button1];
UIBarButtonItem *infoButton2 = [[UIBarButtonItem alloc] initWithCustomView:infoButtonView];
self.navigationItem.rightBarButtonItem = infoButton2;
}
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)];
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.
I am attempting to add a UIToolbar to my cameraOverlayView for a custom imagePickerController. This is the code I want to use:
[self.imagePickerController.cameraOverlayView addSubview:self.topToolbar];
When trying to add a UIToolbar, nothing shows up. Encapsulating it into a UIView and setting it to the view property works if I use:
[self.imagePickerController.cameraOverlayView addSubview:self.view];
But this limits me to using one view for my overlay when I want to add several. I understand I can encapsulate everything into one big view, however when I do that, my bottom toolbar does not appear properly.
Has anyone had success in adding a UIToolbar as a subview to a cameraOverlayView?
i think you need a view to put all the things you need in your cameraOverlay.
for example:
TempView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
TempView.alpha = 1.0;
anImageView1 = [[UIImageView alloc]initWithImage:[UIImage imageNamed:#""]];
anImageView1.frame = CGRectMake(0, 0, anImageView1.image.size.width, anImageView1.image.size.height-50);
anImageView1.hidden = NO;
anImageView1.alpha = 1.0f;
tabBarHolder = [[UIImageView alloc]init];
tabBarHolder.backgroundColor = [UIColor blackColor];
tabBarHolder.frame = CGRectMake(0, 415, 320, 80);
tampBtn = [[UIButton alloc] initWithFrame:CGRectMake(15, 430, 35, 35)];
[tampBtn setBackgroundImage:[UIImage imageNamed:#"iconBack.png"] forState:UIControlStateNormal];
[tampBtn addTarget:self
action:#selector(xup)
forControlEvents:UIControlEventTouchUpInside];
ctampBtn = [[UIButton alloc] initWithFrame:CGRectMake(145, 430, 35, 35)];
[ctampBtn setBackgroundImage:[UIImage imageNamed:#"iconCamera.png"] forState:UIControlStateNormal];
[ctampBtn addTarget:self
action:#selector(takephoto)
forControlEvents:UIControlEventTouchUpInside];
[TempView addSubview:ctampBtn];
[TempView addSubview:tampBtn];
[TempView addSubview:anImageView1];
[TempView addSubview:tabBarHolder];
[TempView bringSubviewToFront:tabBarHolder];
[TempView bringSubviewToFront: ctampBtn];
[TempView bringSubviewToFront:tampBtn];
imagePicker.cameraOverlayView =TempView;
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!