I know how to add a UISegmentedControl to a UIToolBar from within IB, but I am trying to do the same programmatically, because I am using a custom subclass of UISegmentedControl with doesn't have an XIB.
This is the code for the UISegmentedControl:
SVSegmentedControl *navSC = [[SVSegmentedControl alloc] initWithSectionTitles:[NSArray arrayWithObjects:#"List", #"Calendar", nil]];
navSC.delegate = self;
[self.view addSubview:navSC];
[navSC release];
navSC.center = CGPointMake(160, 70);
I was thinking of doing something like [self.toolbar addSubview:navSC], but that didn't show anything.
You need to use the UIToolbar method – setItems:animated: (detailed in the documentation):
UIBarButtonItem *segItem = [[UIBarButtonItem alloc] initWithCustomView:navSC];
UIBarButtonItem *spaceItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:NULL];
[toolBar setItems:[NSArray arrayWithObjects:spaceItem,segItem,spaceItem,nil] animated:YES];
[segItem release];
[spaceItem release];
Related
I want to add two buttons with custom image to Navigation Bar with some specific position.
I found solution But it is for Right/Left Navigation Bar Button.
My code for that is:
NSMutableArray *buttons = [[NSMutableArray alloc] initWithCapacity:2];
UIToolbar *tools = [[UIToolbar alloc]
initWithFrame:CGRectMake(0.0f, 0.0f, 90.0f, 55.01f)];
// Add Pin button.
UIBarButtonItem *bi1 = [[UIBarButtonItem alloc] initWithTitle:#"Edit" style:UIBarButtonItemStylePlain target:self action:#selector(Edit:)];
bi1.style = UIBarButtonItemStyleBordered;
bi1.width = 45;
[buttons addObject:bi1];
[bi1 release];
// Add Hot Spot button.
UIBarButtonItem *bi2 = [[UIBarButtonItem alloc] initWithTitle:#"+" style:UIBarButtonItemStylePlain target:self action:#selector(Add:)];
bi2.style = UIBarButtonItemStyleBordered;
[buttons addObject:bi2];
[bi2 release];
// Add buttons to toolbar and toolbar to nav bar.
[tools setItems:buttons animated:NO];
[buttons release];
// Add toolbar to nav bar.
UIBarButtonItem *twoButtons = [[UIBarButtonItem alloc] initWithCustomView:tools];
[tools release];
self.navigationItem.rightBarButtonItem = twoButtons;
[twoButtons release];
How can i do this?
UIView *vieww =[[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
[vieww addSubview:yourBtn1];
[vieww addSubview:yourBtn2];
[self.navigationController.navigationBar addSubview:vieww];
And if you want to remove yourButtonView then make is global object;
in .h
UIView *vieww;
and in .m
-(void)viewWillDisappear:(BOOL)animated
{
[vieww removeFromSuperview];
}
Or follow this for more Link
if you are using >iOS 5, then use this.
UIBarButtonItem *btn1=[[UIBarButtonItem alloc] initWithTitle:#" + " style:UIBarButtonItemStyleDone target:self action:#selector(action1:)];
UIBarButtonItem *btn2=[[UIBarButtonItem alloc] initWithTitle:#" - " style:UIBarButtonItemStyleDone target:self action:#selector(action2:) ];
self.navigationItem.rightBarButtonItems=[NSArray arrayWithObjects:btn1,btn2,nil];
for < iOS 5 u can use following:
UIToolbar* tools = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 160, 44.01)];
tools.barStyle = UIBarStyleBlackOpaque;
// create the array to hold the buttons, which then gets added to the toolbar
NSMutableArray* buttons = [[NSMutableArray alloc] initWithCapacity:4];
[buttons addObject:btn1];
[buttons addObject:btn2];
[tools setItems:buttons animated:NO];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:tools];
Instead of adding toolbar, you can create one UIView, add two buttons on that view.
UIBarButtonItem *twoButtons = [[UIBarButtonItem alloc] initWithCustomView:yourView];
If you want to do this in combination with using a storyboard, take a look at this question.
I want to draw multiple buttons on a UINavigationBar. These will be either on right side or left side.
I did one example in which I had two buttons ( i.e. Edit and +) on Right side of NaviagationBar.
1) You have to create one NSMutableArray(i.e. "buttons" in example) and add UIBarButtonItem (i.e. bi1 and bi2 in example) to the NSMutableArray (i.e. buttons).
2) Add NSMutableArray(i.e. buttons in example) to toolbar(i.e. UIToolbar *tools in example).
3) Add toolbar to NavigationBar.
NSMutableArray *buttons = [[NSMutableArray alloc] initWithCapacity:2];
UIToolbar *tools = [[UIToolbar alloc]
initWithFrame:CGRectMake(0.0f, 0.0f, 90.0f, 55.01f)];
// Add bar button1.
UIBarButtonItem *bi1 = [[UIBarButtonItem alloc] initWithTitle:#"Edit" style:UIBarButtonItemStylePlain target:self action:#selector(Edit:)];
bi1.style = UIBarButtonItemStyleBordered;
bi1.width = 45;
[buttons addObject:bi1];
//[bi1 release]; Do not release if ARC enabled.
// Add bar button2.
UIBarButtonItem *bi2 = [[UIBarButtonItem alloc] initWithTitle:#"+" style:UIBarButtonItemStylePlain target:self action:#selector(Add:)];
bi2.style = UIBarButtonItemStyleBordered;
[buttons addObject:bi2];
//[bi2 release]; Do not release if ARC enabled.
// Add buttons to toolbar and toolbar to nav bar.
[tools setItems:buttons animated:NO];
//[buttons release]; Do not release if ARC enabled.
// Add toolbar to nav bar.
UIBarButtonItem *twoButtons = [[UIBarButtonItem alloc] initWithCustomView:tools];
[tools release];
self.navigationItem.rightBarButtonItem = twoButtons;
//[twoButtons release]; Do not release if ARC enabled.
do it in your xib file and make properties or just variables in the header
#property (nonatomic, retain) IBOutlet UIBarButtonItem *itemOne;
and then connect it in the xib. Enjoy
Create a new UIToolbar in code and add your buttons to the toolbar. Then set self.navigationItem.rightBarButton to your newly created toolbar (note the example is without ARC so you may need to remove calls to release):
// create a toolbar to have two buttons in the right
UIToolbar* tools = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 100, 44.01)];
// create the array to hold the buttons, which then gets added to the toolbar
NSMutableArray* buttons = [[NSMutableArray alloc] initWithCapacity:3];
// create a standard "add" button
UIBarButtonItem* bi = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:#selector(addRow)];
bi.style = UIBarButtonItemStyleBordered;
[buttons addObject:bi];
[bi release];
// create a spacer
bi = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
[buttons addObject:bi];
[bi release];
[buttons addObject:self.editButtonItem];
// stick the buttons in the toolbar
[tools setItems:buttons animated:NO];
[buttons release];
// and put the toolbar in the nav bar
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:tools];
Here I'm giving you sample code that I used for Button as well as label. you can create button instead of label and image what i created. I hope it will help you
- (void) setLabelForPotraite {
bar = [self.navigationController navigationBar];
[bar setBackgroundColor:[UIColor clearColor]];
barImg=[[UIImageView alloc] initWithImage:[UIImage imageNamed:#"navImg.png"]];
[bar addSubview:barImg];
tick_img_lbl=[[UIImageView alloc]initWithFrame:CGRectMake(86, 6,34, 33)];
tick_img_lbl.image=[UIImage imageNamed:#"tick-1.png"];
[bar addSubview:tick_img_lbl];
[tick_img_lbl release];
tickCount_lbl=[[UILabel alloc] initWithFrame:CGRectMake(126, 2, 50, 40)];
tickCount_lbl.text=#"";
tickCount_lbl.font=[UIFont fontWithName:#"Arial" size:24.0];
[tickCount_lbl setTextAlignment:UITextAlignmentCenter];
tickCount_lbl.font = [UIFont boldSystemFontOfSize:24.0];
tickCount_lbl.textColor=[UIColor whiteColor];
tickCount_lbl.backgroundColor=[UIColor clearColor];
[bar addSubview:tickCount_lbl];
[tickCount_lbl release];
cross_img_lbl=[[UIImageView alloc]initWithFrame:CGRectMake(181, 6, 34, 33)];
cross_img_lbl.image=[UIImage imageNamed:#"x_green.png"];
[bar addSubview:cross_img_lbl];
[cross_img_lbl release];
crossCount_lbl=[[UILabel alloc] initWithFrame:CGRectMake(221, 2, 50, 40)];
crossCount_lbl.text=#"";
crossCount_lbl.font=[UIFont fontWithName:#"Arial" size:24.0];
crossCount_lbl.font = [UIFont boldSystemFontOfSize:24.0];
crossCount_lbl.textColor=[UIColor whiteColor];
[crossCount_lbl setTextAlignment:UITextAlignmentCenter];
crossCount_lbl.backgroundColor=[UIColor clearColor];
[bar addSubview:crossCount_lbl];
[crossCount_lbl release];
master_img_lbl=[[UIImageView alloc]initWithFrame:CGRectMake(269, 6, 34, 33)];
master_img_lbl.image=[UIImage imageNamed:#"thumb.png"];
[bar addSubview:master_img_lbl];
[master_img_lbl release];
}
If any problem in understanding or else then plz inform me.
In my application in some view i have a picker at the bottom and tabBar i successfully hidden the tabBar but the problem is the frame allocated to TabBar is also hiding. so now the picker is displayed only half can any one help me out of this problem ?
use this method it works:
[actionSheetPicker showFromTabBar:self.tabBarController.tabBar];
I hope this helps you.
Don't hide tabbar if you hide it will so many create so many problems in this case
instead of hiding use this
[actionSheet showInView:self.tabBarController.tabBar];
You do not need to hide tabbar controller, you just need to add your action sheet in to tabbarcontroller
[actionSheet showInView:self.tabBarController.view];
Dont Hide tabbar and try bellow code
actionSheet=[[UIActionSheet alloc] init];
[actionSheet showInView:self.tabBarController.tabBar];
actionSheet.frame=CGRectMake(0, 100,480 ,232);
actionSheet.delegate=self;
picker=[[UIPickerView alloc] initWithFrame:CGRectMake(0, 50,480 ,175)];
picker.delegate=self;
picker.showsSelectionIndicator=YES;
[actionSheet addSubview:picker];
UIToolbar *toolbar=[[UIToolbar alloc] initWithFrame:CGRectMake(0, 0,480 ,50)];
toolbar.barStyle =UIBarStyleBlackTranslucent;
NSMutableArray *ButtonArray=[[NSMutableArray alloc ]init];
Save=[[UIBarButtonItem alloc ]initWithTitle:#"Done" style:UIBarButtonItemStyleBordered target:self action:#selector(save_pressed)];
UIBarButtonItem *titleName;
titleName=[[UIBarButtonItem alloc ] initWithTitle: #"Select Date" style:UIBarButtonItemStylePlain target:nil action:nil];
UIBarButtonItem *space=[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
UIBarButtonItem *cancel=[[UIBarButtonItem alloc ]initWithTitle:#"Cancel" style:UIBarButtonItemStyleBordered target:self action:#selector(cancel_pressed)];
[ButtonArray addObject:cancel];
[cancel release];
[ButtonArray addObject:space];
[ButtonArray addObject:titleName];
[titleName release];
[ButtonArray addObject:space];
[space release];
[ButtonArray addObject:Save];
[Save release];
[toolbar setItems:ButtonArray];
[ButtonArray release];
[actionSheet addSubview:toolbar];
[toolbar release];
[actionSheet release];
i want to add 4 buttons in UINAV bar how to do that
should i make them in UINAVBAR controller or navItem or UIBarbutton???
any code will be appreciated
Thanks
UINavigationBar is used to navigate backward or forward in application. You can't use it for any other functionality. How ever if you want to show four button on top use UIToolBar. A tool bar can multiple button with different functionality.
` create toolbar using new
toolbar = [UIToolbar new];
toolbar.barStyle = UIBarStyleDefault;
[toolbar sizeToFit];
toolbar.frame = CGRectMake(0, 410, 320, 50);
//Add buttons
UIBarButtonItem *systemItem1 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd
target:self
action:#selector(pressButton1:)];
UIBarButtonItem *systemItem2 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction
target:self
action:#selector(pressButton2:)];
UIBarButtonItem *systemItem3 = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemCamera
target:self action:#selector(pressButton3:)];
//Use this to put space in between your toolbox buttons
UIBarButtonItem *flexItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace
target:nil
action:nil];
//Add buttons to the array
NSArray *items = [NSArray arrayWithObjects: systemItem1, flexItem, systemItem2, flexItem, systemItem3, nil];
//release buttons
[systemItem1 release];
[systemItem2 release];
[systemItem3 release];
[flexItem release];
//add array of buttons to toolbar
[toolbar setItems:items animated:NO];
[self.view addSubview:toolbar];`
I added a toolBar with this code:
- (void)viewWillAppear:(BOOL)animated {
UIBarButtonItem *yesterday = [[UIBarButtonItem alloc]initWithTitle:#"Yesterday"
style:UIBarButtonItemStyleBordered target:self action:#selector(yesterday:)];
UIBarButtonItem *today = [[UIBarButtonItem alloc]initWithTitle:#"Today"
style:UIBarButtonItemStyleDone target:self action:#selector(today:)];
UIBarButtonItem *tomorrow = [[UIBarButtonItem alloc]initWithTitle:#"Tomorrow"
style:UIBarButtonItemStyleBordered target:self action:#selector(tomorrow:)];
UIBarButtonItem *month = [[UIBarButtonItem alloc]initWithTitle:#"Month"
style:UIBarButtonItemStyleBordered target:self action:#selector(month:)];
NSArray *items = [NSArray arrayWithObjects:yesterday,today,tomorrow,month, nil];
[yesterday release];
[today release];
[tomorrow release];
[month release];
UIToolbar *toolbar = [[UIToolbar alloc] init];
[toolbar sizeToFit];
[toolbar setFrame:CGRectMake( 0, 20, 320, 40)];
[toolbar setItems:items];
[self.navigationController.view addSubview:toolbar];
}
but when i change the view using the navigation controller the toolbar stays there...
how can i remove that subview?
UINavigationController has a toolbar built in which is hidden by default. You can display it using [navigationController setNavigationBarHidden:animated:];. You might want to use that instead. Then, before you push a view controller, set that view controller's hidesBottomBarWhenPushed property to true.
The reason your toolbar doesn't go away in this instance is that you're adding it to navigationController's view which displays on top of other views it controls. You could instead add it as a subview of self.
To answer your specific question, though, to remove the toolbar from any superview, use [toolbar removeFromSuperview]. In this case, I would go with the cleaner solution of using the toolbar that is built into navigation controllers.