How can i rotate a uibarbuttonitem inside a uitoolbar? Because I rotated the uitoolbar (it's vertical now) and my bar button items are rotated too but i dont want that.
thanks
UIBarButtonItem does not extend UIView, so it cannot be transformed directly. You can add the UIBarButtonItem you wish to transform to a UIToolbar, transform the UIToolbar and then add the toolbar as a custom view to another UIBarButtonItem. This item can then be set as a navigation item or added to another UIToolbar.
UIBarButtonItem *backItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemPlay target:self action:#selector(handleForwardItemTouch:)];
UIToolbar *backToolbar = [[[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 44, 44)] autorelease];
[backToolbar setTransform:CGAffineTransformMakeScale(-1, 1)];
UIBarButtonItem *backToolbarItem = [[[UIBarButtonItem alloc] initWithCustomView:backToolbar] autorelease];
self.navigationItem.rightBarButtonItem = backToolbarItem;
Related
I need to add a button to the center of the ToolBar. I have done the adding the button to the toolbar part successfully. My problems are as follows;
1.) I need to center this barbutton. It should be in the center of the Tool Bar
2.) I need to have a text after the refresh button image is displayed.
toolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0 , 320 , 60)];
NSMutableArray* button = [[NSMutableArray alloc] initWithCapacity:1];
UIBarButtonItem *barButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh target:self action:#selector(refreshButtonAction:)];
[button addObject:barButton];
[toolBar setItems:button animated:NO];
[self.view addSubview:toolBar];
1. Add flexible spacers before and after your bar button in the toolbar items array:
toolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0 , 320 , 60)];
UIBarButtonItem *flexibleSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
UIBarButtonItem *barButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh target:self action:#selector(refreshButtonAction:)];
NSArray *toolbarItems = [NSArray arrayWithObjects:flexibleSpace, barButton, flexibleSpace];
[toolBar setItems:toolbarItems animated:NO];
[self.view addSubview:toolBar];
Configuring toolbars is much easier to do in Interface Builder. If your view controller is inside a UINavigationController stack, you can still use IB to create an outlet collection of UIBarButtonItems and set self.toolbarItems in -viewDidLoad.
2. To get custom content in a toolbar you can create a bar button item with a custom view:
UIView *customView = <# anything, could be a UILabel #>;
UIBarButtonItem *customItem = [[UIBarButtonItem alloc] initWithCustomView:customView];
I know this can be done in IB, but I believe if you want to center a button, you will need to add a fixed or flexible space button on either side to keep your button in the middle. If you are going to do this with just code... try and sandwich your button between the 2 space buttons.
When I create and add a UIToolBar and UIBarButton items, how can I get the toolbar and items to appear properly in both orientations. This is what I have so far:
self.toolbar = [[[UIToolbar alloc]initWithFrame:CGRectMake(0, 372, 320, 44)]autorelease];
self.toolbar.autoresizingMask = UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
self.deleteButton = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemTrash target:self action:#selector(deleteButtonClicked)] autorelease];
self.deleteButton.style = UIBarButtonItemStylePlain;
UIBarButtonItem *flexibleSpace = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil] autorelease];
self.shareButton = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:self action:#selector(shareButtonClicked)] autorelease];
self.shareButton.style = UIBarButtonItemStylePlain;
self.navigationItem.leftBarButtonItem = backItem;
NSArray *toolbarItems = [NSArray arrayWithObjects:self.shareButton,flexibleSpace,self.deleteButton,nil];
[self.toolbar setItems:toolbarItems animated:NO];
As you can see from the images, the UIBarButtonItems look correct in portrait, but weirdly squished and off center (especially the trash icon), in landscape. Is there an obvious solution I'm missing, or do I have to do manual resizing to get this right?
It's a subtle difference, but here's what it should look like:
You have to look at the Autoresizing option in the Size inspector both for the UIToolBar and the buttons. UIToolBar might also have a checkmark set for Autoresize subviews uncheck that so it does not influence your buttons.
The answer, at least until iOS 5 is to use the UINavigationController's built in toolbar instead of creating your own. The built in toolbar will resize correctly and automatically.
I want to rotate a UIBarButtonItem.
I have been able to do this with UIButtons using setTransform:CGAffineTransformMakeRotation(…), but UIBarButtonItem does not have this property.
I know the Camera-app does this, so it should be achieveable.
How can I achieve this?
Thanks.
Have you tried using a custom view inside the UIBarButtonItem that you then transform any way you want?
UIBarButtonItem does not extend UIView, so it cannot be transformed directly. You can add the UIBarButtonItem you wish to transform to a UIToolbar, transform the UIToolbar and then add the toolbar as a custom view to another UIBarButtonItem. This item can then be set as a navigation item or added to another UIToolbar. However, if you are using a custom view or image then Emil's approach in the comment above is best.
UIBarButtonItem *backItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemPlay target:self action:#selector(handleForwardItemTouch:)];
UIToolbar *backToolbar = [[[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 44, 44)] autorelease];
[backToolbar setTransform:CGAffineTransformMakeScale(-1, 1)];
UIBarButtonItem *backToolbarItem = [[[UIBarButtonItem alloc] initWithCustomView:backToolbar] autorelease];
self.navigationItem.rightBarButtonItem = backToolbarItem;
You could put a UIButton inside a Bar Button Item, than rotate the UIButton.
I extended UIToolBar, giving access to its subviews, and in it have a function rotate that rotates the buttons in the opposite direction of the bar:
- (void)rotate: (int)degrees{
//for the bar
self.transform=CGAffineTransformMakeRotation(DegreesToRadian(degrees));
//for the subviews (UIBarButtonItems)
for (UIView * subView in self.subviews)
{
if(!CGRectEqualToRect(subView.bounds, self.bounds))
subView.transform = CGAffineTransformMakeRotation(DegreesToRadian(-degrees));
}
}
I'm adding a couple of buttons to an already-existing NavigationController. The two buttons are added to a UIView, which is pushed onto the NavigationItem. The buttons stop and reload a UIWebView.
Problem is that there's a slight offset issue that is making it all look pretty ugly. I wish I could set the UIToolbar to transparent or clear background but that doesn't seem to be an option. Can't seem to use negative offsets either. I've got color matching, but if you look closely there's 1px or 2px of highlighting up top that's causing a visual mismatch and then a slight offset at the bottom.
Some relevant code below (based on this, inbound Googlers). What are my options to resolve this?
// create a toolbar for the buttons
UIToolbar* toolbar = [[UIToolbar alloc]
initWithFrame:CGRectMake(0, 0, 100, 45)];
[toolbar setBarStyle: UIBarStyleDefault];
UIColor *colorForBar = [[UIColor alloc] initWithRed:.72 green:0 blue:0 alpha:0];
toolbar.tintColor = colorForBar;
[colorForBar release];
//[toolbar setTranslucent:YES];
// create an array for the buttons
NSMutableArray* buttons = [[NSMutableArray alloc] initWithCapacity:3];
// create a standard reload button
UIBarButtonItem *reloadButton = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh
target:self
action:#selector(reload)];
reloadButton.style = UIBarButtonItemStyleBordered;
[buttons addObject:reloadButton];
[reloadButton release];
// create a spacer between the buttons
UIBarButtonItem *spacer = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace
target:nil
action:nil];
[buttons addObject:spacer];
[spacer release];
// create a standard delete button with the trash icon
UIBarButtonItem *stopButton = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemStop
target:self
action:#selector(stopLoading)];
stopButton.style = UIBarButtonItemStyleBordered;
[buttons addObject:stopButton];
[stopButton release];
// put the buttons in the toolbar and release them
[toolbar setItems:buttons animated:NO];
[buttons release];
// place the toolbar into the navigation bar
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]
initWithCustomView:toolbar];
[toolbar release];
Update: Alignment was fixed by suggestion by #Jacob below. Now, how to solve the highlighting mismatch?
I've had this same exact issue before, and after some hacking around, I figured this out:
You need to set the height of the UIToolbar's frame to 44.01 for this to work.
EDIT: Re: highlighting mismatch -> You can try setting the backgroundColor to the colorForBar.
UIToolbar* toolbar = [[UIToolbar alloc]
initWithFrame:CGRectMake(0, 0, 100, 44.01)];
UIColor *colorForBar = [[UIColor alloc] initWithRed:.72 green:0 blue:0 alpha:0];
toolbar.tintColor = colorForBar;
toolbar.backgroundColor = colorForBar;
Instead of putting the buttons into a toolbar, you might just make them children views of a parent UIView. You can position a button directly through its frame or center property, without use of spacers, etc.
here is my code....?how to integrate UINavigation bar with UIBarbuttonitem?
UINavigationBar *nav= [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, 320, 60)];
nav.backgroundColor = [UIColor whitecolor];
//nav.tintColor = [UIColor darkGrayColor];
[self.view addSubview:nav];
[nav release];
Try init your item and then
nav.leftBarButtonItem = barButtonItem;
or
nav.rightBarButtonItem = barButtonItem;
You can only have two items on the navigation bar
Note that UIBarButtonItem buttons are properties of the UINavigationItem, not the UINavigationBar.
For example, when using a UINavigationController, it automatically provides its own UINavigationBar. But each of the views managed by the navigation controller can define a UINavigationItem, which controls the title, left and right buttons, etc.
Update: iOS 5 supports multiple buttons on the same side. From the UINavigationItem docs:
Customizing Views
titleView property
leftBarButtonItems property
leftBarButtonItem property
rightBarButtonItems property
rightBarButtonItem property
– setLeftBarButtonItems:animated:
– setLeftBarButtonItem:animated:
– setRightBarButtonItems:animated:
– setRightBarButtonItem:animated:
So, basically, you would write something like this to add two buttons on the left:
UIBarButtonItem *refreshBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh target:self action:#selector(refreshPlans:)];
UIBarButtonItem *selectYearBtn = [[UIBarButtonItem alloc] initWithTitle:#"Select Year" style:UIBarButtonSystemItemAction target:self action:#selector(selectYear)];
self.navigationItem.leftBarButtonItems = [[NSArray alloc] initWithObjects: refreshBtn, selectYearBtn, nil];
Unfortunately, I haven't seen a way to do this in the Storyboard. Hope this helps.