How to mark the UIToolBar autoresze when it rotated - objective-c

I have created a toolbar. Just like this:
UIToolbar *toolbar=[[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, toolBarHeight)];
toolbar.autoresizingMask=UIViewAutoresizingFlexibleWidth;
UIBarButtonItem *itemNull=[[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil] autorelease];
UIBarButtonItem *item0=[[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemRewind target:self action:#selector(pre)] autorelease];
UIBarButtonItem *item1=[[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFastForward target:self action:#selector(pre)] autorelease];
UIBarButtonItem *item2=[[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:self action:#selector(pre)] autorelease];
UIBarButtonItem *item3=[[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemReply target:self action:#selector(pre)]
autorelease];
UIBarButtonItem *item4=[[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh target:self action:#selector(pre)] autorelease];
itemNull.width=frame.size.width-itemwidth*5;
item0.width=itemwidth;
item1.width=itemwidth;
item3.width=itemwidth;
item4.width=itemwidth;
item2.width=itemwidth;
[toolbar setItems:[NSArray arrayWithObjects: itemNull,item0,item1,item2,item3,item4, nil]];
[self addSubview:toolbar];
[toolbar release];
It is added to a view. But the items are out of the sign when I rotated the view.
and I want to know how to make it visible and not to reset it's frame.
Did the items have the Property like autoresize?

there is an inbuilt hidden toolbar..
[self.navigationController setToolbarHidden:NO animated:YES]; // i hope its a navigation based application.
modify code
UIBarButtonItem *itemNull=[[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil] autorelease];
UIBarButtonItem *item0=[[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemRewind target:self action:#selector(pre)] autorelease];
UIBarButtonItem *item1=[[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFastForward target:self action:#selector(pre)] autorelease];
UIBarButtonItem *item2=[[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:self action:#selector(pre)] autorelease];
UIBarButtonItem *item3=[[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemReply target:self action:#selector(pre)]
autorelease];
UIBarButtonItem *item4=[[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh target:self action:#selector(pre)] autorelease];
[self.navigationController setToolbarHidden:NO animated:YES];
self.toolbarItems = [NSArray arrayWithObjects: itemNull, item0, item1, item2, item3, item4, nil];
hope it helps. happy coding :)

Related

UIPickerView toolbar buttons not working

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];

Centering on a UIToolBar with a UIToolbarButton on either side

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:

how do you add a right button to a uinavigationcontrol

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 am not able to add UIBarButtonItem in UINavigationController

navigator=[[UINavigationController alloc]initWithRootViewController:contacts];
UIBarButtonItem *nextButton = [[UIBarButtonItem alloc]
initWithTitle:#"Delete"
style:UIBarButtonItemStyleBordered
target:self
action:#selector(makeCall)];
[[self.navigator navigationItem] setLeftBarButtonItem:nextButton];
[nextButton release];
I am not able add UIBarButtonItem in UINavigationController ,please help
Speaking off the cuff here, perhaps instead of
[[self.navigator navigationItem] setLeftBarButtonItem:nextButton];
use
self.navigator.navigationBar.items = [NSArray arrayWithObject:nextButton];
to do what you want.

Getting Memory allocation problem at UIBarButtonItem in Iphone sdk

Here I am getting memory allocation problem at UIBarButtonItem and the related code for that is:
toolbar = [UIToolbar new];
toolbar.barStyle = UIBarStyleBlackOpaque;
[toolbar setFrame:CGRectMake(0, 350,320,20)];
[self.view addSubview:toolbar];
UIBarButtonItem* barItem1 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:#selector(categoryConfig:)] ;
rightBarItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemBookmarks target:self action:#selector(dialogOtherAction:)] ;
UIBarButtonItem* barItem2 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:#selector(categoryConfig:)] ;
NSArray *items = [NSArray arrayWithObjects: barItem1,rightBarItem,barItem2, nil];
[barItem1 release];
[barItem2 release];
[rightBarItem release];
[toolbar setItems:items animated:NO];
after adding UIBarButtonItems into the array items I released them.even though its showing allocations at barbuttons.
can any help me for this?
Thank you,
Monish.
You allocated toolbar with toolbar = [UIToolbar new];, so you need to release it.