I've never added a segmented control to a bottom toolbar without Interface Builder. I added the same segmented control to the navigation control toolbar without this issue. What am I doing wrong? Thank you.
//enable UIToolbar
self.navigationController.toolbarHidden = NO;
//UISegmentedControl
NSArray *segmentedControlItems = [[NSArray alloc] initWithObjects:#"Day One",#"Day Two",nil];
self.segmentedCon = [[UISegmentedControl alloc] initWithItems:segmentedControlItems];
[segmentedControlItems release];
[self.segmentedCon addTarget:self action:#selector(daySegConIndexChange) forControlEvents:UIControlEventValueChanged];
[self.segmentedCon setSelectedSegmentIndex:0];
[self.segmentedCon setSegmentedControlStyle:UISegmentedControlStyleBar];
[self.segmentedCon setWidth:85.0 forSegmentAtIndex:0];
[self.segmentedCon setWidth:85.0 forSegmentAtIndex:1];
[self.segmentedCon setFrame:[self.navigationController.toolbar bounds]];
//bar button items
UIBarButtonItem *flexibaleSpaceBarButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
UIBarButtonItem *segBarBtn = [[UIBarButtonItem alloc] initWithCustomView:self.segmentedCon];
//nsarray of tool bar items
NSArray *toolbarItems = [NSArray arrayWithObjects:flexibaleSpaceBarButton,segBarBtn,flexibaleSpaceBarButton,nil];
[self setToolbarItems:toolbarItems animated:YES];
[segBarBtn release];
Try this instead..
//UISegmentedControl
NSArray *segmentedControlItems = [[NSArray alloc] initWithObjects:#"Day One",#"Day Two",nil];
self.segmentedCon = [[UISegmentedControl alloc] initWithItems:segmentedControlItems];
[segmentedControlItems release];
[self.segmentedCon addTarget:self action:#selector(daySegConIndexChange) forControlEvents:UIControlEventValueChanged];
[self.segmentedCon setSelectedSegmentIndex:0];
[self.segmentedCon setSegmentedControlStyle:UISegmentedControlStyleBar];
//bar button items
UIBarButtonItem *flexibaleSpaceBarButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
UIBarButtonItem *segBarBtn = [[UIBarButtonItem alloc] initWithCustomView:self.segmentedCon];
Try setting the frame of the segmented control...
CGRect r = self.segmentedCon.frame;
self.segmentedCon.frame = CGRectMake(r.origin.x, r.origin.y, r.size.width, 30);
Related
I have added array of bar button to the navigation items using the property rightBarButtonItems,it work good for iOS5,when i tested in iOS6 only one bar button item is visible.
UIBarButtonItem *updateButton = [[UIBarButtonItem alloc]
initWithTitle:#"Update"
style:UIBarButtonItemStylePlain
target:self
action:#selector(updateData)];
UIBarButtonItem *refreshButton = [[UIBarButtonItem alloc]
initWithTitle:#"Refresh"
style:UIBarButtonItemStylePlain
target:self
action:#selector(refresh)];
NSArray *arrBtns = [[NSArray alloc]initWithObjects:updateButton,refreshButton, nil];
self.navigationItem.rightBarButtonItems=arrBtns;
Is there any new property for iOS6 to add the array of bar button to navigationitem.
Any help would be appreciated,Thanks a lot.
Please use the segmentController on the rightBarButtonItems if you want to add multiButton on rightBarButtonItems of NavigationBar
UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] initWithItems:
[NSArray arrayWithObjects:#"Add",#"Delete",
nil]];
segmentedControl.frame = CGRectMake(0, 0, 80, 30);
segmentedControl.segmentedControlStyle = UISegmentedControlStyleBar;
[segmentedControl setWidth:35.0 forSegmentAtIndex:0];
[segmentedControl setWidth:45.0 forSegmentAtIndex:1];
[segmentedControl addTarget:self action:#selector(segmentAction:) forControlEvents:UIControlEventValueChanged];
segmentedControl.momentary = YES;
UIBarButtonItem *segmentBarItem = [[UIBarButtonItem alloc] initWithCustomView:segmentedControl];
[segmentedControl release];
self.navigationItem.leftBarButtonItem = segmentBarItem;
[segmentBarItem release];
Secondly add the second button on other side of the first bar Button.
This blue "OK" button is common in a UIWebview when you click in a webform at iPhone.
Are there a easy way to recreate it in code? Or I will have to create it in a hard way?
The most close code was:
UISegmentedControl *buttonOK = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObject:#"OK"]];
[buttonOK setSegmentedControlStyle:UISegmentedControlStyleBar];
[buttonOK setTintColor:[UIColor colorWithRed:0.25f green:0.51f blue:0.95f alpha:1.0f]];
[buttonOK setFrame:CGRectMake(276, 8, 38, 30)];
But isn't the same...
The button in the web view uses a translucent UIToolbar with a done button style:
UISegmentedControl* segmentedControl = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:#"Previous", #"Next", nil]];
segmentedControl.segmentedControlStyle = UISegmentedControlStyleBar;
segmentedControl.tintColor = [UIColor blackColor];
segmentedControl.momentary = YES;
UIBarButtonItem* segmentedControlItem = [[UIBarButtonItem alloc] initWithCustomView:segmentedControl];
UIBarButtonItem* item = [[UIBarButtonItem alloc] initWithTitle:#"OK" style:UIBarButtonItemStyleDone target:self action:#selector(done:)];
UIBarButtonItem* flexSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:NULL];
UIToolbar* toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 320.0f, 44.0f)];
toolbar.barStyle = UIBarStyleBlack;
toolbar.translucent = YES;
toolbar.items = [NSArray arrayWithObjects:segmentedControlItem, flexSpace, item, nil];
[self.view addSubview:toolbar];
I've got very simple code here
[mImagePickerToolBar setBarStyle:UIBarStyleBlackTranslucent];
[mImagePickerToolBar sizeToFit];
UIBarButtonItem *spaceItem1 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
UIBarButtonItem *spaceItem2 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
UIBarButtonItem *cameraItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCamera target:self action:#selector(captureBarItemPressed:)];
NSArray *items = [NSArray arrayWithObjects: spaceItem1, cameraItem, spaceItem2, nil];
[mImagePickerToolBar setItems:items animated:NO];
on my UIToolBar camera item is not centered at all, see on the picture, in comparison with home button its a little bit right, why is this so and how to center it ?
The flexible spaces should work. This is a bug in iOS, in my opinion. You can see for yourself by mocking this up in interface builder.
Here is the bordered button:
And here is the plain button:
It isn't even lined up within its own selection area.
[self.navigationController setToolbarHidden:NO];
toolbar = [[UIToolbar alloc] init];
toolbar.barStyle = UIBarStyleBlackOpaque;
[toolbar sizeToFit];
toolbar.frame = CGRectMake(0, 436, 320, 44);
UIBarButtonItem *spaceItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
spaceItem.width = 130.0;
UIBarButtonItem *cameraItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCamera target:self action:#selector(captureBarItemPressed:)];
NSArray *items = [NSArray arrayWithObjects: spaceItem, cameraItem, nil];
[toolbar setItems:items];
[self.navigationController.view addSubview:toolbar];
This is working for me. Please make sure at your side
I am already creating multiple buttons but I don't know how to align the buttons.
My code is here:
- (void)viewDidLoad
{
//self.title=#"Asset Management";
[super viewDidLoad];
listOfItems = [[NSMutableArray alloc] init];
[listOfItems addObject:#"User Information"];
[listOfItems addObject:#"Regional Settings"];
[listOfItems addObject:#"Configuration"];
toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 20, 320, 44)];
//UIToolbar* toolbar = [[UIToolbar alloc] initWithFrame:CGs
toolbar.tintColor = [UIColor clearColor];
[toolbar setTranslucent:YES];
// create the array to hold the buttons, which then gets added to the toolbar
NSMutableArray* buttons = [[NSMutableArray alloc] initWithCapacity:3];
// Create button1
UIBarButtonItem *propertiesButton = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemSearch target:self action:#selector(button1Pressed)];
[buttons addObject:propertiesButton];
[propertiesButton release];
// Create button2
UIBarButtonItem *commentaryButton = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemCompose target:self action:#selector(button2Pressed)];
[buttons addObject:commentaryButton];
[commentaryButton release];
// Create button3
UIBarButtonItem *versionsButton = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:#selector(button3Pressed)];
[buttons addObject:versionsButton];
[versionsButton release];
// stick the buttons in the toolbar
[toolbar setItems:buttons animated:NO];
//self.toolbarItems = buttons;
[buttons release];
// and put the toolbar in the nav bar
[[self navigationItem] setRightBarButtonItem:[[[UIBarButtonItem alloc] initWithCustomView:toolbar] autorelease]];
[toolbar release];
}
How do I create space b/w the buttons. Pls help me.
Thanks in advance!
You can add spaces between tool bar items using either of the two built-in space button types
UIBarButtonSystemItemFixedSpace and UIBarButtonSystemItemFlexibleSpace.
Fixed Space button
UIBarButtonItem *fixedSpace = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace
target:nil
action:nil];
[fixedSpace setWidth:20];
Flexible Space button
UIBarButtonItem *flexibleSpace = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace
target:nil
action:nil];
Add the space bar buttons in between the other tool bar items.
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];`