How to animate UIBarButtonItem to slide on the UIToolbar from the left? - ios

In my iOS app I have a media player with UIToolbar for the controls. I want to make UIBarButtonItem slide from the left onto the UIToolbar as I touch on the player screen.
This is what I've tried, and it does add the UIBarButtonItem from the left, but there is no animation part.
// create new button
UIBarButtonItem* b = [[UIBarButtonItem alloc] initWithTitle:#"b"
style:UIBarButtonItemStyleBordered
target:self
action:nil];
NSMutableArray* temp = [toolbar.items mutableCopy]; // store the items from UIToolbar
NSMutableArray* newItems = [NSMutableArray arrayWithObject:b]; // add button to be on the left
[newItems addObjectsFromArray:temp]; // add the "old" items
[toolbar setItems:newItems animated:YES];
Any kind of help is highly appreciated!

I had a similar issue where the designer wanted that kind of animation in a navbar.
Assuming that your app does not need the other buttons to move, then you can do it like this:
// create a UIButton instead of a toolbar button
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setTitle:#"b" forState:UIControlStateNormal];
// Save the items *before* adding to them
NSArray *items = toolbar.items;
// Create a placeholder view to put into the toolbar while animating
UIView *placeholderView = [[UIView alloc] initWithFrame:button.bounds];
placeholderView.backgroundColor = [UIColor clearColor];
[toolbar setItems:[items arrayByAddingObject:[[UIBarButtonItem alloc] initWithCustomView:placeholderView]]
animated:NO];
// get the position that is calculated for the placeholderView which has been added to the toolbar
CGRect finalFrame = [toolbar convertRect:placeholderView.bounds fromView:placeholderView];
button.frame = CGRectMake(-1*button.bounds.size.width, finalFrame.origin.y, button.bounds.size.width, button.bounds.size.height);
[toolbar addSubview:button];
[UIView animateWithDuration:duration
animations:^{ button.frame = finalFrame; }
completion:^(BOOL finished) {
// swap the placeholderView with the button
[toolbar setItems:[items arrayByAddingObject:[[UIBarButtonItem alloc] initWithCustomView:button]]
animated:NO];
}];
If your app does need to move the other buttons, then it's a bit trickier b/c you have to use only customView bar button items and get the initial position of all of them, pull them into the toolbar (and out of the list of items), animate them, and then put everything back. (Easy, right?) Good luck!

Related

Is it possible to place two buttons on left side of navigation bar with transformation of most left button on OnClick?

I want to display two buttons or can say two images on left side of navigation bar with same TouchUpInSide method.
When I click on that two buttons the left most button should transform more to left and when i again click on the two buttons the left most button come back to original position.
Or another way:
I can place two UIImageView on left side of navigation bat and place one button with clear background and give event on that.
So is it possible and How?
If you want to add just simple UIButtons you can use the following method.
1) Create multiple UIButton objects
2) Add all those buttons to a UIView object
3) Create UIBarButtonItem and pass the UIView as a custom view
Code is as follows:
// create the container view
UIView* viewContainer = [[UIView alloc] init];
// Create buttons and add it to the container view
UIButton* btnAdd = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[btnAdd setTitle:#"Add" forState:UIControlStateNormal];
[viewContainer addSubview:btnAdd];
UIButton* btnRefresh = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[btnRefresh setTitle:#"Refresh" forState:UIControlStateNormal];
[viewContainer addSubview:btnRefresh];
// now create a Bar button item with custom view
UIBarButtonItem* barBtnItem = [[UIBarButtonItem alloc] initWithCustomView:viewContainer];
// Set the navigation bar's right button item
self.navigationItem.rightBarButtonItem = barBtnItem;
or else if you want to add bar UIBarButtonItems go with adding multiple barbuttons as Anbu.Karthik said.
Here is the code to add multiple bar buttons items.
// Create UIToolbar to add two buttons in the right
UIToolbar* toolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 133, 44.01)];
// Create standard "add" button
UIBarButtonItem* btnAdd = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:NULL];
btnAdd.style = UIBarButtonItemStyleBordered;
// Create spacer
UIBarButtonItem* spacer = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
// create standard "refresh" button
UIBarButtonItem* btnRefresh = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh target:self action:#selector(btnRefresh_Clicked:)];
btnRefresh.style = UIBarButtonItemStyleBordered;
// stick the buttons in the UIToolbar
[toolBar setItems:#[btnAdd, spacer, btnRefresh] animated:NO];
// Put the toolbar in the UINavigationBar
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:toolBar];
Hope this will help you. :)

Trouble adding custom button to navigation controller's toolbar

I have a tableview controller that has a navigation controller. I have added a toolbar to the bottom of the view and now I need to add a custom button. I have the following code (from viewDidLoad), which is producing strange results. When the view loads, I see the custom button for a brief second, then an empty toolbar. At the moment I am only trying to get one custom button to load.
self.navigationController.toolbar.barStyle = UIBarStyleBlackTranslucent;
[self.navigationController.toolbar sizeToFit];
CGFloat toolbarHeight = [self.navigationController.toolbar frame].size.height + 20;
[self.navigationController.toolbar setFrame:CGRectMake(CGRectGetMinX(self.view.bounds),
CGRectGetMaxY(self.view.bounds) - toolbarHeight,
CGRectGetWidth(self.view.bounds),
toolbarHeight)];
UIBarButtonItem *btn = [[UIBarButtonItem alloc] initWithImage:[UIImage
imageNamed:#"stub_fav.png"]
style:UIBarButtonItemStylePlain
target:self action:#selector(addFavorite)];
[btn setTintColor:[UIColor whiteColor]];
[btn setImage:[UIImage imageNamed:#"stub_fav.png"]];
NSArray *toolbarItems = [[NSArray alloc] initWithObjects:btn, nil];
[self.navigationController.toolbar setItems:toolbarItems];
Any ideas? Thanks!
Im not sure what btnAddFave is for but it's not the subview of anything. I assume btw and btnAddFave should have the same background image, if so you spelled stub_fav.png wrong. shouldn't it be stub_fave.png

UIView animation causes a UIButton to jump

UI is programmatically generated in my app. I have few buttons and texboxes in the UI. I have developed a custom uidatapicker object, which pops up from the top and animates to the middle of the screen. When uidatapicker pops up I draw another UIView(called helper view) with the size of the screen so all other uiobjects in the screen except uidatepicker become disabled.
But when the UIDatePicker is animating a button in the UI jumps to another location. Also I have three buttons in my UI. It happens with only one button(one UIButon in the UIView). Other two buttons are ok. Also there is no significant difference between those buttons except the button text.
I removed the earlier described view(helper view), but still the
problem is occurring.
I need to know why this is occurring how to
prevent it.
Also I have lot of other pages which works fine.
The code
-(void)openEditDateTime:(UIDatePickerMode) mode {
if ([clientView viewWithTag:9]) {
[self cancelPressed];
}
CGRect toolbarTargetFrame = CGRectMake((clientView.frame.size.width/2)-160, (clientView.frame.size.height/2)+91, 320, 44);
CGRect datePickerTargetFrame = CGRectMake((clientView.frame.size.width/2)-160, (clientView.frame.size.height/2)-125, 320, 216);
backgroundView = [[UIView alloc] initWithFrame:clientView.bounds];
backgroundView.alpha = 0;
backgroundView.backgroundColor = [UIColor blackColor];
backgroundView.tag = 9;
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(cancelPressed:)];
[backgroundView addGestureRecognizer:tapGesture];
[clientView addSubview:backgroundView];
[self bringToFront:backgroundView];
datePicker = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, self.bounds.size.height+44, 320, 216)];
datePicker.tag = 10;
datePicker.datePickerMode = mode;
[clientView addSubview:datePicker];
[clientView bringSubviewToFront:datePicker];
[datePicker becomeFirstResponder];
[self bringToFront:datePicker];
if(date != nil){
datePicker.date = date;
}
toolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, self.bounds.size.height, 320, 44)];
toolBar.tag = 11;
toolBar.barStyle = UIBarStyleBlackTranslucent;
UIBarButtonItem *spacer = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:#selector(donePressed:)];
UIBarButtonItem *cancelButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:#selector(cancelPressed:)];
[toolBar setItems:[NSArray arrayWithObjects:spacer, doneButton, cancelButton, nil]];
[clientView addSubview:toolBar];
[clientView bringSubviewToFront:toolBar];
[UIView beginAnimations:#"MoveIn" context:nil];
toolBar.frame = toolbarTargetFrame;
datePicker.frame = datePickerTargetFrame;
backgroundView.alpha = 0.5;
[UIView commitAnimations];
}
- clientView is a UIScrollView.
- backgroundView is the helper view described earlier.
This is how I add buttons. I wil put only a part of the button rendering code as putting all the code is unnecessary and it has lot other dependencies as well.
-(RenderedElement*)renderElement:(NSObject*)element:(ParentView*)parent:(PageView*)page:(Page*)modelPage:(RenderedElement*)parentRenderedElement {
UIButton *buttonView = nil;
Button *templateElement = nil;
buttonView = [UIButton buttonWithType:UIButtonTypeCustom];
[buttonView setLineBreakMode:UILineBreakModeWordWrap];
[buttonView addTarget:parent action:#selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
[parent addSubview:buttonView];
}
UPDATE
When I change the order of rendering and if I render the buttons first the jumping effect is not happening. The UI works fine. It temporally solves the problem. But I want find the reason and have a better solution.
Maybe you can add
[UIView setAnimationBeginsFromCurrentState:YES];
after
[UIView beginAnimations:#"MoveIn" context:nil];
Did you actually say which button is moving? Kind of hard to help without some more detail. But here are some suggestions to look at:
Rather than adding the datepicker to the bottom of the stack and then pulling it forward:
[clientView addSubview:datePicker];
[clientView bringSubviewToFront:datePicker];
Why don't you try to try adding it by:
[clientView insertSubview:datePicker atIndex:0];
Also, you are adding the toolbar, which I assume contains the offending button, after adding the datPicker and then pulling that forward. Do you want the toolbar to be above the datepicker in the view hierachy?
Perhaps insert it at:
[clientView insertSubview:toolBar aboveSubview:datePicker];
Finally, do you need to animate the toolbar frame?
The only thing i can imagine is that may be you have this button on the client view. and since you are adding and moving this
[clientView addSubview:datePicker];
[clientView bringSubviewToFront:datePicker];
this may cause readjusting of your UIButton's frame. and since that happens during this procedure it also animates its movement which may appear as a jumping button..so i guess you should try
[jumpingButton setAnimationsEnabled:NO]; //you-know jumping button is the outlet of the -you-know-who

dismissing a UIPickerView once the user selected all rows

I have a UIPickerView with 2 components where the user can select a font and a font size.
I want to dismiss the view once the user has selected both.
Can I somehow do this using the didSelectRow:inComponent method?
Yes, if you record the component that is provided to the method then when all of your components have had a selection made you can dismiss.
Note that users may not like it very much and adding a toolbar above the picker with a done button might work better.
I would suggest creating a container view to hold the picker and the toolbar (subviews), then show / hide the container view with whatever animations you like (instead if where you currently show the picker view). The picker and the toolbar don't need to be linked in any way, just laid out nicely in the container.
I hope it will help you
UIActionSheet *action = [[UIActionSheet alloc] init];
action.actionSheetStyle = UIActionSheetStyleBlackTranslucent;
UIPickerView *pickerFromage=[[UIPickerView alloc]initWithFrame:CGRectMake(0, 44, 0 , 0)];
UIToolbar *pickerAgeToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
pickerAgeToolbar.barStyle = UIBarStyleBlackOpaque;
[pickerAgeToolbar sizeToFit];
NSMutableArray *barItems = [[NSMutableArray alloc] init];
UIBarButtonItem *flexSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
[barItems addObject:flexSpace];
UIBarButtonItem *doneBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:#selector(PickerDoneClick)];
[doneBtn setTintColor:[UIColor blackColor]];
[barItems addObject:doneBtn];
[pickerAgeToolbar setItems:barItems animated:YES];
pickerFromage.delegate=self;
pickerFromage.dataSource=self;
pickerFromage.tag=value;
pickerFromage.showsSelectionIndicator=YES;
[action addSubview:pickerAgeToolbar];
[action addSubview:pickerFromage];
[action showInView:self.view];
[action setBounds:CGRectMake(0,0, 320, 464)];`
-(void)PickerDoneClick{
[action dismissWithClickedButtonIndex:0 animated:YES];
action=nil;
}
You can do it this way:
Take a UIView in xib and then add a UIPickerView and a toolbar with UIButton on it. Give the delegate to the UIPickerView and assign an action to the UIButton. Take an outlet of UIView and in your implementation file(ie. .m file). Hide the UIView and assign the frame to the bottom of the screen. Lets say you have named the view as myPickerView.
myPickerView.frame = CGRectMake(CGRectGetMinX(self.view.bounds),
CGRectGetMaxY(self.view.bounds),
CGRectGetWidth(myPickerView.frame),
CGRectGetHeight(myPickerView.frame));
myPickerView.hidden = YES;
After doing this you can animate the view from bottom to upwards by setting the frame again in animation block, and can also unhide it.
When you press done, again animate the view and set the frame as above.
Below is the code for animating
- (IBAction)showPicker:(UIButton *)sender {
self.myPickerView.hidden = NO;
[UIView animateWithDuration:0.5 animations:^{
[self.myPickerView setFrame:CGRectMake(0,
100,
CGRectGetWidth(self.myPickerView.frame),
CGRectGetHeight(self.myPickerView.frame))];
}];
}
action for done button:
-(IBAction)doneClicked:(id)sender {
self.myPickerView.hidden = NO;
[UIView animateWithDuration:0.5 animations:^{
[self.myPickerView setFrame:CGRectMake(CGRectGetMinX(self.view.bounds),
CGRectGetMaxY(self.view.bounds),
CGRectGetWidth(self.myPickerView.frame),
CGRectGetHeight(self.myPickerView.frame))];
}];
}
Hope this helps.

UIButton not showing in iOS

Following is a code to display a button to the toolbar.
UIButton myButton = [UIButton buttonWithType:UIButtonTypeCustom];
UIImage *myImage = [UIImage imageNamed:#"img1.png"];
[myButton setBackgroundImage:myImage forState:UIControlStateNormal];
UIBarButtonItem *myToolbarButton = [[UIBarButtonItem alloc] initWithCustomView:myButton];
Nothing gets displayed on the toolbar. Please tell me what is wrong.
[myToolbar setItems:[NSArray arrayWithObject:myToolbarButton]]; // might help
OR if it's a navigation bar
self.navigationItem.rightBarButtonItem = myToolbarButton;
Also you should set the frame for the custom button, something like this:
UIButton *mybutton = [[UIButton alloc] initWithFrame:CGRectMake:0,0,200,50];
Then if you want to add a selector/callback:
[myButton addTarget:self action:#selector(someMethod) forControlEvents:UIControlEventTouchUpInside];
self.navigationItem.leftBarButtonItem = myButtonItem; // and change for the right button.
Where self is a UIViewController.
You forgot to add this button to the toolbar!
NSArray *toolbarItems = [[NSArray alloc] initWithObjects:myToolbarButton,nil]; // you can more buttons here
[aToolbar setItems:toolbarItems animated:NO]; // change to YES if you want to have nice animation
[toolbarItems release];
If you want to add a button to navigation bar, use this:
[self.navigationItem setRightBarButtonItem:myToolbarButton]; // or setLeft...
Also, you should remember to set the frame of myButton:
[myButton setFrame:CGRectMake(0, 0, 30, 32)];

Resources