I'm trying to add a custom image on a standard UIButton. I know of the setBackgroundImage and setImage methods to do that. However, this will remove the 'standard' borders! Using UIButtonTypeCustom or UIButtonTypeRoundRect gives the same results...
Can this be so hard?
A normal button like those in the navigationbar - just a custom image instead of the titleLabel. Do I really have to draw the button itself in an image editor?
Yes you do have to draw a custom image.
Well you can add a subview of image to the UIButtonRoundRect. So just create a RoundRect button the usual way:
UIButton *button = [UIButton buttonWithType:UIButtonRoundRect];
then create an image view with your image and add it as subview to the button:
UIImageView *iv = [[UIImageView alloc] initWithImage:(UIImage *)image];
[button addSubivew:iv];
That should do the trick. Plus you can also set the position of that imageview by [iv setCenter:CGPointMake(x,y)];
Hope that helps.
Related
I would like to reproduce a Tweetbot like navigation bar.
I'm still searching if it's possible to place an UIButton in place of title in an UINavigationBar bar and to make it fits perfectly between right and left buttons like it is done in Tweetbot application.
When I try to do this, over a certain size the self.navigationItem.titleView is resized down automatically
Tell me if I miss something evident,
Thank you
I provide two screenshots to let you see what I'm talking about
They've probably rolled in their own implementation of UINavigationBar and/or UINavigationController. I guess it's not worth trying to hack UIKit that much, since it will be very fragile and not future-proof.
You can add a button to the navigation item's title view property, which accepts an UIView, and since a UIButton is a subclass of an UIView this is not an issue.
Alternatively, if you are using storyboard, just drag a button to the center of the navigation bar, it's that easy.
// Create the button, and specify the type
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
// Set the button's frame
button.frame = CGRectMake(0.0f, 0.0f, 30.0f, 30.0f);
// Set your custom image for the button
[button setImage:[UIImage imageNamed:#"YOUR_IMAGE_NAME"] forState:UIControlStateNormal];
// Set the button's action, which is the method that will be called when it is pressed
[button addTarget:self action:#selector(doSomething:) forControlEvents:UIControlEventTouchUpInside];
// Add the button to the navigation bar's title view
[self.navigationItem.titleView addSubview:button];
SDK Reference:
https://developer.apple.com/library/ios/#documentation/UIKit/Reference/UINavigationItem_Class/Reference/UINavigationItem.html
Have you try to set autoresizesSubviews to YES on titleView, then set the correct autoresizing masks on the UIButton? If this doesn't work I suggest you to create your custom view subclass and override -layoutSubviews to fit your needs.
I have placed a UIButton inside a static grouped cell and tried to make it fit to the cell border, but get this kind of double-lines:
I tried changing the separator style, but it didn't help. Is there a way to make the border-lines of the button invisible?
Thanks in advance.
From a design perspective, it might be better to add the button as a footer view for the table instead of embedded in a cell. Take a look at Calendar app on the iPhone. When you edit an existing event, they add a button at the bottom of the table for deleting the event. Theirs is a bit fancier than a simple rounded rect button, but that's trivial to do as well.
Embed your login button in a generic UIView and add it as such:
UIView *footerView = [[UIView alloc] initWithFrame:...];
UIButton *logInButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
// do button set up here, including sizing and centering, and add to footer view
[footerView addSubview:logInButton];
self.tableView.tableFooterView = footerView;
[footerView release];
Change button type to custom and it will get rid of the Round Rectangle around the button.
how to Change UIbutton background color??
I try it on IB, but it doesn't work. only changes it for custom button.
I would like to change the white color on the rounded rect button.
Or instead how to make a custom button whit rounded corners
thanks!
Rounded rect button's color can be changed to any color with the background color property in IB as well as through coding, but can not change its color to transparent color easily. For that, you better use a custom button with either rounded image or set layer as demonstrated in the example and don't forget to add Quartz Core framework in you project as well as to import it in your class.
UIBUtton *myBtn = [UIButton buttonWithType:UIbuttonTypeCustom];
myBtn.frame = frame; //your desired size
myBtn.clipsToBounds = YES;
myBtn.layer.cornerRadius = 5.0f;
[self.view addSubView:myBtn];
You may find it difficult to adjust the default button. However you can use any view you want with a custom button.
Here's an article on creating views with rounded corners:
http://iphonedevelopment.blogspot.com/2008/11/creating-transparent-uiviews-rounded.html
You also may look at a program called Opacity, which allows you to create a a lot of customized standard iOS interface art/buttons.
http://likethought.com/opacity/
UIButton *tagButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
and have a look at this... that my do the trick.. i did it for mine.
You can use a custom UIButton with custom images for backgroundimage for each state. Just make the different states of your rounded button in photoshop for example.
You can use Custom button But for that use the image of button of white rounded corners
so it will be look like white round rectangle button
You should use UIButtonTypeCustom, instead of any other.
When I add an icon to a UIBarButtonItem via the Interface Builder, the icon is displayed white. When I add the same icon file programmatically to another UIToolbar, the icon is displayed black. Why?
UIImage *image = [UIImage imageNamed:#"icon.png"];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setImage:image forState:UIControlStateNormal];
rootViewController.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithCustomView:reloadButton] autorelease];
Everything Jongsma said is right, you should use the initWithImage:style: message.
The next problem is not the way you create the UIBarButtonItem, but the place you assign it. You create it with UIBarButtonItemStylePlain, which should normally render the icon's outline in white, but the rightBarButtonItem of a UINavigationItem (just like the left) is not allowed the UIBarButtonItemStylePlain. It's implicitly converted to UIBarButtonItemStyleBordered. In the bordered style the icon is rendered 'as is', which is black with a slight gradient.
I think if you want the item in white on a bordered barButton, you'll have to touch the image itself.
Answer: If you want it white, color your image white.
Details:
UIBarButtonItems behave a little differently depending on how you use them.
When adding to a UIToolbar:
initWithImage:style:target:action: creates "white icons" (image color is ignored, opaque pixels are used as a mask to create a white image).
This is true for bordered and plain styles (but on UIToolbar only).
initWithCustomView: displays normal colored image.
When adding to a UINavigationItem:
initWithImage:style:target:action: creates colored images and converts plain to bordered.
In your code, you are setting an UIButton as the subview of an UIBarButtonItem.
UIBarButtonItem is already a button, so you shouldn't add another button as the subview.
Try this:
UIImage *image = [UIImage imageNamed:#"icon.png"];
rootViewController.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithImage:image] autorelease];
Had the same problem. I Noted that the #2X images were used instead...
I'd like to control visibility on a simple UI element. If it was a button, I'd just call setHiden on it, but UIImage has no such method.
What's the preferred way of showing or hiding an image? Just making it a UIButton and not hooking it up to any methods seems wasteful.. is that really the right way to do it?
Shouldn't your image be in an image view?
UIImageView imageView = [[UIImageView alloc] initWithImage:myImage];
Then you can hide the image view with:
imageView.hidden = YES;
hidden is a property of UIView, so you can use it with UIImageViews.