Is there a way to make a button transparent without alpha?
To explain: I have a background image on my view. There is also a button with an image background. If the image has a transparent part I see white inside the button's background image.
Is it possible to make the image transparent?
You can set the button type to 'Custom', either in code with: UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; or in IB on the Attributes pane, under Button -> Type -> Custom.
Set the backgroundColor of the button to [UIColor clearColor] and optionally its opaque to NO (Should automatically be applied by setting the background color to transparent).
You want to set the button opaque property to NO.
In InterfaceBuilder, you have a checkbox to do that.
Related
I have created in the XIB a custom UIButton with no image. When the button is clicked, the title disappears.
These answers didn't help me because, I do not have any image that can overlap the text
After push a button it's title disappears
iOS - Interface Builder: UIButton title disappears when setting image
UIButton with custom background loses title
This didn't help either, because I do not have any background color
UIButton title disappears
This one didn't help too
Button changes title every time pressed
UIButton and its subclasses have 4 states which can change by code or stage config in IB
normal
highlighted
selected
disabled
Check UIButton.h in UIKit framework and you'll see how to use them:
- (void)setTitle:(NSString *)title forState:(UIControlState)state;
- (void)setTitleColor:(UIColor *)color forState:(UIControlState)state;
- (void)setImage:(UIImage *)image forState:(UIControlState)state;
- (void)setBackgroundImage:(UIImage *)image forState:(UIControlState)state;
If you setTitle, image, or backGroundImage for normal state, the default one of other states are same as normal one.
Default state is Normal, other state can be set:
[button setHighlighted:YES];
[button setSelected:YES];
[button setEnable:NO];//disable state
Button change from Normal to Highlighted on click, so if you want to keep normal title, please check:
-Don't use setImage or config image for normal and hightlighted state(just use backgroundImage)
-Don't setTitle:#"" forState:UIControlStateHighlighted or config to
nothing in IB.
-Don't setTitleColor or choose titleColor in IB same as UIButton backgroundColor.
You can test with 4 different titles and backGroundImages for 1 button and know how can it display.
In an XIB, you can set the title for each of the different states by changing the State Config and then setting the title for the corresponding state. In the following example, I have set the Default state title to Title and the Highlighted state title to a single space, which makes it appear as blank in the app. Note however, that the preview only shows the Default settings, and does not update for the different configurations (see images below).
Default State Title
Highlighted State Title
I have had this happen as well. What I believe the issue is, is since the button is default a white rounded button with the default "highlight" state of a button, and inverts the text color as the button is pressed, it does the same with a custom button. Now, since the text is a blue, and the background is white for a rounded button, it is quite the same in a custom button. The custom button's text will be whatever you want(set) it to be. The background is usually a transparent color as well, and since when the custom button is highlighted, it causes the text color to be inverted, just as a regular button does. This causes the textcolor to become transparent, and the background of the custom button just doesn't change because there is nothing to change to, hence the fact that the button is custom, (meaning you must set the highlight states and other attributes of the button). I have had this happen before, and I never really realized this before, but I hope this helps you out!
// for setting text in normal state
[_myButton setTitle:#"myText" forState:UIControlStateNormal];
// show another text on touch
[_myButton setTitle:#"myText" forState:UIControlStateHighlighted];
I would like to make a generic class that when tapped, makes the element grayish.
Facebook's app is the perfect example of what I want to achieve. All their links and images become gray when tapped.
I can only guess that they are subclassing UIButton.
I have made my button's style UIButtonTypeCustom to get rid of the rounded border. Beyond this, I don't know how to have the gray overlay because I see no such property in the documentation.
Its simple:
#define TAG_GRAYVIEW 5671263 // some random number
// add the gray overlay
UIView *grayView = [[UIView alloc] initWithFrame:button.bounds];
grayView.backgroundColor = [UIColor grayColor];
grayView.tag = TAG_GRAYVIEW;
[button addSubview:grayView];
// remove the gray overlay
UIView *grayView = [button viewWithTag:TAG_GRAYVIEW];
[grayView removeFromSuperview];
I think you need to use a semi transperant grey image PNG file. You need to then set Image of button in Highlighted state.
Also note that both the images for Normal State and Highlighted State need to have the images with titles on them.
As once we set the image to button, btn.titleLabel.text won't be displayed.
So you can have a image with transperant background and title on it for Normal state. And an grey image with title on it for Highlighted State.
Code for doing it programmatically is
[btn setImage:#"Transperant.png" forState:UIControlStateNormal];
[btn setImage:#"Grey.png" forState:UIControlStateHighlighted];
Hope this helps you.
The default UIButton masks the opaque content using a gray highlight. For example when using a transparent png, only the parts that contain non-transparent pixels will be grayed out on touch. The default UIButton highlight has no effect on subviews/sublayers, and will only work on provided images. What you see in the Facebook app is probably just a UIWebView highlight, possibly customized using css.
To create something similar using your own control (to prevent the overhead of a UIWebView) you should probably create your own UIControl subclass and highlight the content on touch using a transparent CALayer. You can even mask the CALayer to only highlight the actual contents, instead of a rectangular shape.
Also see my stackoverflow post on creating custom controls for iOS by subclassing UIControl.
Try setting the button up something like this.
*mind you I didn't create the image for this example.
Set your button type to custom, and in "State Config" select "Highlighted" from there you will want to set the image of the button to be a semi-transparent grey image. There are probably several other ways to achieve this same effect but this one should be nice and simple.
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.
I would like to create a custom Info type button. For the halo effect that goes over the button Image, do I place an ImageView ontop? Or can i try to add the halo with setImage? I tried setImage, but it seems when u try to set the halo, which is larger than the actual button, the halo gets resized to the button's bounds. I tried disabling clipsToBounds, autoresizing mask, and setting contentMode for the button..but none seem to work.
Neither. All you have to do is create a generic button (either with [UIButton buttonWithType:UIButtonTypeCustom] or just [[UIButton alloc] initWithFrame:...] and set its showsTouchOnHighlight property to YES.
I have a view with a button and three UITextFields. The view's background is an image. I want UITextField's and Round Rect Button's backgrounds to be transparent, so the have their functions and display text, but there are no backgrounds. Adding a transparent image as a background doesn't help.
Thanks in advance!
As pheelicks said for UIButton can create it with type UIButtonTypeCustom.
For UITextField set its border style to UITextBorderStyleNone.
For the button you need to change the button type to:
button.buttonType = UIButtonTypeCustom;
See this question
As for the textfields, I don't think you can make them transparent. One solution would be to set the background image of each textfields to the image that would be showing through if the textfields were transparent. If you get it pixel perfect then it'll look as if the textfields are transparent
[[UIButton buttonWithType:UIButtonTypeCustom] initWithFrame:CGRectMake(0, 0, 100, 100)];