I am trying to center some text on a UIToolBar with a button on either side, but I am having some problems.
I have the following code:
UIToolbar *toolBar = [[UIToolbar alloc] initWithFrame:toolbarInitialFrame];
toolBar.barStyle = UIBarStyleBlackTranslucent;
UILabel * label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 200, 25)];
[label setText:#"Title"];
[label setBackgroundColor:[UIColor clearColor]];
[label setTextColor:[UIColor whiteColor]];
[label setFont:[UIFont boldSystemFontOfSize:16]];
UIBarButtonItem * labelEmu=[[UIBarButtonItem alloc] initWithCustomView:label];
UIBarButtonItem *cancelButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:#selector(cancelAlarmAndDismissDatePicker:)];
UIBarButtonItem *spacer = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:#selector(setAlarmAndDismissDatePicker:)];
So what I want is the cancel button (cancelButton) on the left, the title (labelEmu) centered in the middle, and the done button (doneButton) on the right. Something like this:
|[Cancel] Title [Done]|
I was hoping I could achieve that with this:
[toolBar setItems:[NSArray arrayWithObjects: cancelButton, spacer, labelEmu, spacer, doneButton, nil]];
But unfortunately what I get is this:
|[Cancel]Title [Done]|
After some playing about, Ive found that this line:
[toolBar setItems:[NSArray arrayWithObjects: cancelButton, spacer, labelEmu, spacer, nil]];
...produces this:
|[Cancel] Title |
Where the title is centered, ignoring the fact that the cancel button is there. But when I do this:
[toolBar setItems:[NSArray arrayWithObjects: spacer, labelEmu, spacer, doneButton, nil]];
...it produces this:
| Title [Done]|
Where the title is centered in the space between the far left edge and the right side of the done button. It is not ignoring the done button in the same way it ignores the cancel button.
Can anybody point me in the right direction to get what I am after? Sorry about all the naff ASCII diagrams! :)
Maybe try using:
[[UIBarButtonItem alloc] initWithTitle:#"yourtitle" style:UIBarButtonItemStylePlain target:nil action:nil];
It might help if the UITollbar will be in charge on the label.
EDIT
Try adding:
[label sizeToFit];
Before adding it to the tollBar
---EDITED---
After further investigation - turns out problem really was label. You have set it's width to 200.
Should be a lot less for it to autoresize correctly.
See this code:
UIToolbar *toolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 200, 320, 50)];
toolBar.barStyle = UIBarStyleBlackTranslucent;
[self addSubview:toolBar];
UILabel * label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 300, 25)];
[label setText:#"Title"];
[label setBackgroundColor:[UIColor clearColor]];
[label setTextColor:[UIColor whiteColor]];
[label setFont:[UIFont boldSystemFontOfSize:16]];
[label sizeToFit];
UIBarButtonItem * labelEmu=[[UIBarButtonItem alloc] initWithCustomView:label];
UIBarButtonItem *cancelButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:#selector(cancelAlarmAndDismissDatePicker:)];
UIBarButtonItem *spacer = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:#selector(setAlarmAndDismissDatePicker:)];
[toolBar setItems:[NSArray arrayWithObjects: cancelButton, spacer, labelEmu, spacer, doneButton, nil]];
It generated something like this:
Related
I am trying to set the next/previous buttons on my keyboard toolbar, I want get put in navigation to go next/Previous textfield. Here is what I am trying.
How can I use the system bar button item to change to textfield?
-(void)addTeam{
UIView * view = [[UIView alloc]initWithFrame:CGRectMake(15, (0+(50*[teamsArray count])), 273, 50)];
[view setTag:[teamsArray count]+1];
UILabel *lblTeamName = [[UILabel alloc ] initWithFrame:CGRectMake(5,0,58,20)];
[lblTeamName setBackgroundColor:[UIColor clearColor]];
lblTeamName.font = [UIFont fontWithName:#"Arial Rounded MT Bold" size:(12.0)];
lblTeamName.text = [NSString stringWithFormat:#"Team %i",[teamsArray count]+1];
[lblTeamName setTag:7];
[view addSubview:lblTeamName];
UITextField* txtTeamName = [[UITextField alloc]initWithFrame:CGRectMake(5,20,150,30)];
[txtTeamName setBorderStyle:UITextBorderStyleRoundedRect];
[txtTeamName setPlaceholder:#"Team Name"];
[txtTeamName addTarget:self action:#selector(hideKeyboard) forControlEvents:UIControlEventEditingDidEndOnExit];
[txtTeamName setDelegate:self];
[view addSubview:txtTeamName];
UIButton *btnDelete = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[btnDelete addTarget:self
action:#selector(btnDelete_OnTouch:)
forControlEvents:UIControlEventTouchDown];
[btnDelete setTag:[teamsArray count]];
[btnDelete setTitle:#"-" forState:UIControlStateNormal];
btnDelete.frame = CGRectMake(160, 20, 20, 30);
[view addSubview:btnDelete];
[scrollView addSubview:view];
[teamsArray addObject:view];
}
-(BOOL)textFieldShouldBeginEditing: (UITextField *)textField
{
UIToolbar * keyboardToolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 40)];
keyboardToolBar.barStyle = UIBarStyleDefault;
[keyboardToolBar setItems: [NSArray arrayWithObjects:
[[UIBarButtonItem alloc]initWithTitle:#"Previous" style:UIBarButtonItemStyleBordered target:self action:#selector(previousTextField)],
[[UIBarButtonItem alloc] initWithTitle:#"Next" style:UIBarButtonItemStyleBordered target:self action:#selector(nextTextField)],
[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil],
[[UIBarButtonItem alloc]initWithTitle:#"Done" style:UIBarButtonItemStyleDone target:self action:#selector(resignKeyboard)],
nil]];
textField.inputAccessoryView = keyboardToolBar;
}
- (void)nextTextField {
????
}
-(void)previousTextField
{
????
}
you could use next and previous button,
How to navigate through textfields (Next / Done Buttons)
And do this in ViewDidLoad
UIToolbar * keyboardToolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 40)];
keyboardToolBar.barStyle = UIBarStyleDefault;
[keyboardToolBar setItems: [NSArray arrayWithObjects:
[[UIBarButtonItem alloc]initWithTitle:#"Previous" style:UIBarButtonItemStyleBordered target:self action:#selector(previousTextField)],
[[UIBarButtonItem alloc] initWithTitle:#"Next" style:UIBarButtonItemStyleBordered target:self action:#selector(nextTextField)],
[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil],
[[UIBarButtonItem alloc]initWithTitle:#"Done" style:UIBarButtonItemStyleDone target:self action:#selector(resignKeyboard)],
nil]];
textField.inputAccessoryView = keyboardToolBar;
I'm trying to add a toolbar to a UIPicker.
I've seen that the right way to go is this way:
UIToolbar* toolbarWeight= [[UIToolbar alloc] initWithFrame:CGRectMake(0,0,320,44)];
UIBarButtonItem *barButtonDone1 = [[UIBarButtonItem alloc] initWithTitle:#"Done"
style:UIBarButtonItemStyleBordered target:self action:#selector(gotoNextField:)];
[toolbarWeight setItems:[NSArray arrayWithObject:barButtonNext1] animated:YES];
weightPickerView = [[UIPickerView alloc]init];
[weightPickerView addSubview:toolbarWeight];
While:
-(IBAction)gotoNextField:(id)sender{
NSLog(#"Works");
}
The picker works just fine but the button doesn't work.
After doing some research, I've tried this approach:
(I suspected that the problem was related to the UIBarButtonItem action)
UIButton* myBtn = [[UIButton alloc]init];
myBtn.frame = CGRectMake(0,0,50,24);
[myBtn addTarget:self action:#selector(gotoNextField:) forControlEvents:UIControlEventTouchUpInside];
[myBtn setTitle:#"Next!" forState:UIControlStateNormal];
[myBtn setBackgroundColor:[UIColor orangeColor]];
UIBarButtonItem *barButtonNext1 = [[UIBarButtonItem alloc] initWithCustomView:myBtn];
[toolbarWeight setItems:[NSArray arrayWithObject:barButtonNext1] animated:YES];
Doesn't work either. The button appears but doesn't get selected/respond to touchUpInside.
Here an example for adding a UIPickerView including a Toolbar - works with iOS 7
Make sure to implement the interfaces UIPickerViewDataSource, UIPickerViewDelegate and also implement the #selector methods
pickerView = [[UIPickerView alloc] init];
[pickerView setDataSource: self];
[pickerView setDelegate: self];
pickerView.showsSelectionIndicator = YES;
UIToolbar *toolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithTitle:#"Done" style:UIBarButtonItemStyleBordered target:self action:#selector(itemWasSelected:)];
UIBarButtonItem *cancelButton = [[UIBarButtonItem alloc] initWithTitle:#"Cancel" style:UIBarButtonItemStyleBordered target:self action:#selector(logoutData)];
UIBarButtonItem *flexible = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
UIBarButtonItem *clearButton = [[UIBarButtonItem alloc] initWithTitle:#"Clear" style:UIBarButtonItemStyleBordered target:self action:#selector(clearData)];
[toolBar setItems:[NSArray arrayWithObjects:cancelButton, clearButton, flexible, doneButton, nil]];
pickerParentView = [[UIView alloc] initWithFrame:CGRectMake(0, 60, 320, 260)];
[pickerParentView addSubview:pickerView];
[pickerParentView addSubview:toolBar];
[self.view addSubview:pickerParentView];
As adding a toolbar to the pickerView, the toolbar is added behind the picker preventing the buttons from responding to touch events.
Encapsulating both the picker and the toolbar in a view, and making it an inputView (to make both the picker and the toolbar popup together) provides the desired solution.
weightPickerView = [[UIPickerView alloc]initWithFrame:CGRectMake(0, 44, 320, 216)];
weightPickerView.tag = 13;
UIView* genericWeightView = [[UIView alloc]initWithFrame:CGRectMake(0, 219, 320, 266)];
[genericWeightView addSubview:toolbarWeight];
[genericWeightView addSubview:weightPickerView];
and then:
[weight_txtField setInputView:genericWeightView];
I'm working with Objective-C for the first time, and have an odd bug in an application that was given to me. The following pictures demonstrate the problem I'm having:
This screen displays when the user rotates the iPad. This is normal. The control at the top (<) allows the user to hide the subview on the left side. Upon hiding the subview, the dark blue portion should expand to fill the screen. Prior to my working with the application, this worked. Now, this is happening:
I haven't found anything to explain why, exactly, this is happening. Any ideas?
- (void)viewDidLoad {
[super viewDidLoad];
[self addGestureRecognizers];
// initialize the "<" button...
toggleButton = [[UIBarButtonItem alloc] initWithTitle:[PanoUtils leftIndicator] style:UIBarButtonItemStylePlain target:self action:#selector(toggleScreen)];
// the annotate button...
annotateButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:#"Pencil.png"] style:UIBarButtonItemStylePlain target:self action:#selector(annotateTapped)];
// the done button for annotations...
doneButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:#selector(doneAnnotateTapped)];
// a flexible spacer...
nameFieldSpacerButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
// the accelerometer button...
alignButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:#"gyro.png"] style:UIBarButtonItemStylePlain target:self action:#selector(align:)];
// and the search button.
searchButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:#"Search.png"] style:UIBarButtonItemStylePlain target:self action:#selector(searchButtonTouched:)];
// customize the align button after the user selects it.
UIImage *buttonImage = [UIImage imageNamed:#"gyroSelected.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(align:) forControlEvents:UIControlEventTouchUpInside];
alignSelButton = [[UIBarButtonItem alloc] initWithCustomView:button];
NSMutableArray *itemsArray = [toolBar.items mutableCopy];
[itemsArray addObject:searchButton];
[itemsArray addObject:annotateButton];
[itemsArray addObject:alignButton];
[toolBar setItems:itemsArray animated:NO];
[itemsArray release];
[self adjustToggleButton];
}
How can I add a button to a UINavigationControl that I added to a ViewController? (The project isn't UINavigationControler based.)
Here's the code I've added to my viewDidLoad method:
navigation = [[UINavigationController alloc] init];
navigation.view.frame = CGRectMake(0, 0, 1024, 748);
navigation.navigationBar.tintColor = [UIColor blackColor];
navigation.navigationBar.alpha = 1.0f;
navigation.navigationBar.translucent = NO;
UIBarButtonItem *cancelButton = [[UIBarButtonItem alloc] initWithTitle:#"Cancel" style:UIBarButtonItemStylePlain target:self action:#selector(cancel)];
[navigation.navigationItem setRightBarButtonItem:cancelButton animated:YES];
[cancelButton release];
[self.view addSubview:navigation.view];
[self pushPresentationList];
Can someone help me fix lines 7-9 please?
UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithTitle:#"Button"
style:UIBarButtonSystemItemDone target:nil action:nil];
UINavigationItem *item = [[UINavigationItem alloc] initWithTitle:#"Awesome"];
item.rightBarButtonItem = rightButton;
item.hidesBackButton = YES;
[navigationBar pushNavigationItem:item animated:NO];
Do you need an UINavigationController? Why not just use a simple UINavigationBar. Here is some sample code:
UINavigationBar *navBar = [[UINavigationBar alloc] initWithFrame:
CGRectMake(0,0,self.view.frame.size.width, 49.0f)];
[self.view addSubview:navBar];
UIBarButtonItem *cancelButton = [[UIBarButtonItem alloc] initWithTitle:#"Cancel"
style:UIBarButtonItemStylePlain target:self action:#selector(cancel)];
UINavigationItem *rightBarButtonItem = [[UINavigationItem alloc] init];
[rightBarButtonItem setRightBarButtonItem:cancelButton];
[navBar pushNavigationItem:rightBarButtonItem animated:NO];
I add this navigation bar code but i am not able to click on the left side lat item button "Edit" and when i replace with textbox even that is also not work (not take value), if you can figure that out what is the wrong with this code i will be very help full to you.
Thanks,
Aashish
find the screen shot
http://www.flickr.com/photos/71234685#N02/6440926473/in/photostream
-(void) initializeNavigationalBar {
UIToolbar* _leftNavBarTools = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 300, 44.01)];
// create the array to hold the buttons, which then gets added to the toolbar
NSMutableArray* _leftNavBarButtons = [[NSMutableArray alloc] initWithCapacity:4];
UIBarButtonItem* _editScenarioProduct = self.editButtonItem;
UIBarButtonItem* _addTurftype = [[UIBarButtonItem alloc] initWithTitle:#"Turftype" style:UIBarButtonItemStyleBordered target:self action:#selector(getTurfType)];
UIBarButtonItem *_addTargetN = [[UIBarButtonItem alloc] initWithTitle:#"Target N" style:UIBarButtonItemStyleBordered target:self action:#selector(getTargetNperWeek)];
UIBarButtonItem *_addMapRegion = [[UIBarButtonItem alloc] initWithTitle:#"Map Region" style:UIBarButtonItemStyleBordered target:self action:#selector(getMapRegion)];
CGRect _textFrame = CGRectMake(0, 0, 80, 30 );
_turfacresTextField = [[UITextField alloc] initWithFrame:_textFrame];
_turfacresTextField.borderStyle = UITextBorderStyleRoundedRect;
_turfacresTextField.font = [UIFont systemFontOfSize:12.0];
_turfacresTextField.placeholder = #"Turf Acres";
_turfacresTextField.textAlignment = UITextAlignmentCenter;
_turfacresTextField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
_turfacresTextField.autocorrectionType = UITextAutocorrectionTypeNo; // no auto correction support
_turfacresTextField.keyboardType = UIKeyboardTypeDefault;
_turfacresTextField.returnKeyType = UIReturnKeyDone;
UIBarButtonItem *_addTurfacres = [[UIBarButtonItem alloc] initWithCustomView:_turfacresTextField];
[_leftNavBarButtons addObject:_addTurftype];
[_leftNavBarButtons addObject:_addTargetN];
[_leftNavBarButtons addObject:_addMapRegion];
[_leftNavBarButtons addObject:_addTurfacres];
[_leftNavBarButtons addObject:_editScenarioProduct];
[_leftNavBarTools setItems:_leftNavBarButtons animated:NO];
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:_leftNavBarTools];
UIToolbar* _rightNavBarTools = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 200, 44.01)];
// create the array to hold the buttons, which then gets added to the toolbar
NSMutableArray* _rightNavBarButtons = [[NSMutableArray alloc] initWithCapacity:4];
UIBarButtonItem *_addSoilSetting = [[UIBarButtonItem alloc] initWithTitle:#"Settings" style:UIBarButtonItemStyleBordered target:self action:#selector(setSoilandCarryover)];
[_rightNavBarButtons addObject:_addSoilSetting];
UIBarButtonItem* _addApplication = [[UIBarButtonItem alloc] initWithTitle:#"Add Appplication" style:UIBarButtonItemStyleBordered target:self action:#selector(addApplication)];
[_rightNavBarButtons addObject:_addApplication];
[_rightNavBarTools setItems:_rightNavBarButtons animated:NO];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:_rightNavBarTools];
}
Try [self editButtonItem] instead of self.editButtonItem.