Mimic XCode UIBarButtonItem init with an image - objective-c

I was trying to create programmatically a UIBarButtonItem with an image.
I went like this :
[[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:#"L-large-star#2x.png"] style:UIBarButtonItemStylePlain target:nil action:nil]
With that method my icon gets upscaled (and it's ugly). When I create that same button from the Interface Builder, the icon is a the right size (1/2 of the button).
Any idea how I could mimic this ?

Related

UISearchBar embedded in UIBarButtonItems (Only available in iPad documents)?

I'm trying to insert an UISearchBar to my Toolbar of my TableView but it fail to built my app (iOS5.1) with this error
UISearchBar embedded in UIBarButtonItems (Only available in iPad documents)
http://img.muse-gaming.org/file/1359259573-uisearchbar.png
And i really can't figure it out...
Any idea ?
That being said, it might be possible to make your own view that looks and works like a UIToolbar that does allow a UISearchBar.
Solution is that Use a navigation bar instead of a toolbar. Set the search bar to the navigation bar's title view.
Or Yu can add it Programmatically
self.searchBar.frame = CGRectMake(70, 3, 230, 44);
UIToolbar * searchToolbar = [[UIToolbar alloc]initWithFrame:CGRectMake(0,0+20,[self view].bounds.size.width,52)];
UIBarButtonItem *flexibleSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
UIBarButtonItem *infoButtonItem=[[UIBarButtonItem alloc]initWithTitle:#"Update" style:UIBarButtonItemStyleBordered target:self action:#selector(infoButtonClicked)];
UIBarButtonItem * searchBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:self.searchBar];
[searchToolbar setItems:[NSArray arrayWithObjects:flexibleSpace,infoButtonItem,searchBarButtonItem, nil] animated:YES];
[self.view addSubview:searchToolbar];
Apple fixed this in Xcode 8.2. I think they disabled it before because popovers were not allowed on iOS before iOS 8.0 and a search bar in a toolbar implies that a popover will be used most times.

Navigation Bar or ToolBar trouble in xcode

Ok I was looking at the iPhone iCal app, and was curious whether that top bar is a navigation bar or a toolbar? ive tried using both, but also i cannot figure out how to change the size of the buttons to be as small as the + button in the top right... very confused.. I'm assuming its a navigation bar, but when i read the description of the navigation bar, it said that whenever you are to add a button or item onto the bar, you cannot connect it directly... no idea how else to do it... but anyone wanna help with this issue?
If you are mentioning about this one
It is not UITabBar , it is UINavigationBar, the button on extreme left is inbuilt backbutton of UINavigationBar and the that at right is an extra button that you can add , its clearly shown in this question , and to change the type (ie, + button) you can simply change the button style using
UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithTitle:#"Done"
style:UIBarButtonSystemItemAdd target:nil action:nil];
adding button to UINavigationBar
UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithTitle:#"Done"
style:UIBarButtonSystemItemAdd target:nil action:nil];
rightButton.width=10;
rightButton.height=10;
UINavigationItem *item = [[UINavigationItem alloc] initWithTitle:#"Title"];
item.rightBarButtonItem = rightButton;
item.hidesBackButton = YES;
[bar pushNavigationItem:item animated:NO];
[rightButton release];
[item release];
But normally you would have a navigation controller, enabling you to write:
UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithTitle:#"Done"
style:UIBarButtonSystemItemAdd target:nil action:nil];
self.navigationItem.rightBarButtonItem = rightButton;
[rightButton release];
Hope this helps,
regards
The top bar with RED circle is UINavigationaBar & the bar with GREEN circle is custom designed.
You can the use the below written code to add the system defined Add button to UINavigationaBar
UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithTitle:#"Done"
style:UIBarButtonSystemItemAdd target:nil action:nil];
I realized that in fact you do not need code to get the button to shrink to the size of the + button in the top right corner of the calendar app. In fact, once you are in the storyboard. Open up the utilities tab on the right. Then open the attributes inspector. and where it says Identifier, the drop down tab has options. Choose the add option.

Change UIBarButtonItem into Simple Icon

I am trying to display the "settings gear" instead of the word "Settings" in by navigation bar. Does anyone know how to do this? Thank you!
UIBarButtonItem *btnGo = [[UIBarButtonItem alloc] initWithTitle:#"Settings"
style:UIBarButtonItemStyleBordered target:self action:#selector(settingsAction)];
self.navigationItem.rightBarButtonItem = btnGo;
Unfortunately, there is no default way to get this image. You should use initWithImage:style:target:action:
and add an image of your own. You can make one yourself, or get pre-rolled from some where like Glyphish: http://www.glyphish.com
you can try making a UIButton first , setting the gear image on it and than
UIBarButtonItem *btnGo = [[UIBarButtonItem alloc] initWithCustomView:btn];

setRightBarButtonItems iOS5 not working

I am using the new API for UINavigationItem setRightBarButtonItems like this:
UIBarButtonItem *buttonSettings = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:#"settings.png"]
style:UIBarButtonItemStylePlain
target:self
action:#selector(showSettings:)];
UIBarButtonItem *buttonLogout = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemPageCurl target:self action:#selector(showSettings:)];
[[self navigationItem] setRightBarButtonItems:[NSArray arrayWithObjects:buttonLogout, buttonSettings, nil]];
But, only 1 button (settings) is appearing and logout button is not appearing at all. What am I doing wrong?
Thanks
You are not allowed to do it:
UIBarButtonSystemItemPageCurl
The system page curl button.
This bar button image can be used only for bar button items placed on toolbars.
Available in iOS 4.0 and later.
Declared in UIBarButtonItem.h.

How to make UIBarButtonSystemItem bordered?

I have play UIBarButtonItem on UIToolbar. I want to make the style bordered. As it is system item, is there a way to make it bordered style?
UIBarButtonItem *systemItem1 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemPlay target:self action:#selector(play:)];
Set the UIBarButtonItem's style property:
systemItem1.style = UIBarButtonItemStyleBordered;
See the UIBarButtonItem class reference.