I'm adding a UIButton as a subView in a popup and I can't get it to show up semi-transparently. The background for the main view of the popup does show up semi-transparently, but this UIButton stays fully opaque. Can someone tell my why the code below is not working? Thanks.
UIButton* checkButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
checkButton.frame = CGRectMake(187, 288, 30, 30);
[checkButton setTitle:#"?" forState:UIControlStateNormal];
checkButton.opaque = NO;
[checkButton setBackgroundColor:[UIColor colorWithWhite:1.0 alpha:0.3]];
[checkButton setTitleColor: [UIColor colorWithRed: 51 / 255.0 green:0 blue: 153 / 255.0 alpha:0.5] forState: UIControlStateNormal];
[checkButton addTarget:self action:#selector(check) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:checkButton];
The background for a RoundedRect button is the rectangle in which it is located, and not the button itself. Try changing the background color to red or something equally visible and you will see that the background is only visible between the rounded corners of the button and the rectangular frame in which the button is set. Sadly, you cannot change the color of the button, which is what most people think they are doing when they change the backgroundColor property.
To change the button color, you will need to use UIButtonTypeCustom.
try setting your UIColor to
myButton.layer.backgroundColor = [UIColor x..]
you'll need a
#import <QuartzCore/QuartzCore.h>
im pretty sure the colour in the rounded rect button is in the layer, not in the regular background, this is why people struggle to change its colour..
alternately, put the alpha on the views colour back to 1 and hit the views alpha property, that'll certainly do the lot in one move.
PengOne is right – setting the background color doesn't change the color of the rounded rect. It only affects the corners around the rect.
But you can also just set the button's alpha property to something less than one. Try 0.7 to make the view semi-transparent.
Try using UIButtonTypeCustom instead UIButtonTypeRoundedRect as the button type.
UIButtonTypeRoundedRect is weird. Setting background /alpha etc on it only changes the background of the rectangle into which the button is rendered.
You could change the button type to UIButtonTypeCustom and then use QuartzCore to set cornerRadius to make it appear rounded.
Related
Is it possible to have a custom UIButton that has an image covering only half of it?
I imagine it would be something like this:
UIButton *someButton = [UIButton buttonWithType:UIButtonTypeCustom];
[someButton setBackgroundImage:[UIImage imageNamed:#"buttonPicture"]];
/* Then there must be some property I can set that prevents the button from stretching the image?? */
.
.
.
I want to have a 50x100px button which only has a 50x50px image on the top half, and to be transparent on the bottom half.
I know how to create a custom button and everything. Im just woondering what the property is that controls the stretching of the backgroundImage.
Thanks!
Use
[someButton setImage:[UIImage imageNamed:#"buttonPicture"] forState: UIControlStateNormal];
someButton.imageEdgeInsets = UIEdgeInsetsMake(-50, 0, 0, 0);
I think you could:
create your button image as a 50x100px image (same size as the button);
make this image be half transparent (PNG with alpha),
instead of trying to assign the button a smaller image covering only half of it and prevent stretching.
In my project, we are customizing the UITextField for getting the icon image in the right side of the textField(like the bookmark icon in browser).
The functionality is,
1) the textField shouldn't be editable
2) Initially the right icon in black color.
3) IF user starts click, the icon image to be changed to Orange color.
4) If user releases, the icon image turned into its original state (black color)
First 2 points were implemented.
For 3rd point overridden the UIControl::beginTrackingWithTouch method and changed the icon color to Orange.
Requesting your help for implementing the last point. I am unable to get the release event in order to change the black color image.
Tried using touchesEnded but this is called only when release done when user comes out from the control.
anyone help me how to identify when the user click ends.
Thanks and Regards.
IIRC, the user click and hold the bookmark icon ? if yes maybe you can use UIButton for that:
UIImage *normalImage = [UIImage imageNamed:#"normal_image.png"];
UIImage *touchedImage = [UIImage imageNamed:#"touched_image.png"];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(0, 0, 50, 50); // put your frame here
[button setImage:normalImage forState:UIControlStateNormal]; // Normal state image
[button setImage:touchedImage forState:UIControlStateHighlighted]; // Touched state image
Override below methods:
(void)endTrackingWithTouch:(UITouch *)touch withEvent:(UIEvent *)event;
(void)cancelTrackingWithEvent:(UIEvent *)event;
I want to make a UIButton with type UIButtonTypeCustom (for the shape). I want to assign the title using button.titleLabel because I need to specify the font. The following code looks like it should work, but doesn't -- no label shows up, period.
UIImage *editButtonImage = [UIImage imageNamed: #"editButton.png"];
float width = editButtonImage.size.width;
float height = editButtonImage.size.height;
UIButton *editButton = [UIButton buttonWithType: UIButtonTypeCustom];
editButton.frame = CGRectMake(0, 0, width, height);
[editButton setBackgroundImage: editButtonImage forState: UIControlStateNormal];
editButton.adjustsImageWhenHighlighted = YES;
editButton.titleLabel.text = #"Edit";
editButton.titleLabel.textColor = [UIColor whiteColor];
editButton.titleLabel.textAlignment = UITextAlignmentCenter;
editButton.titleLabel.font = [UIFont fontWithName: #"Helvetica" size: 14];
[self.view addSubview: editButton];
Everyone always says to use setTitle:forState: but that gives you a font I don't like. The titleLabel method is NOT deprecated -- it should work.
I have run into this several times before and always just worked around it, but I'd really like to figure it out. Any ideas?
Setting the titleLabel's text property like that has no effect. Instead, call -setTitle:forState: on the button:
[editButton setTitle:#"Edit" forState:UIControlStateNormal]
The reason for this is because the button can have different titles for different states (e.g., UIControlStateDisabled, UIControlStateHighlighted). Setting a property for the UIControlStateNormal control state will apply to all the states if you don't specify the others explicitly.
Per the documentation for UIButton:
This class provides methods for setting the title, image, and other appearance properties of a button. By using these accessors, you can specify a different appearance for each button state.
You can customize label's color and shadow color based on the state. See -setTitleColor:forState and -setTitleShadowColor:forState, respectively. The rest of the properties on titleLabel, such as textAlignment and font, should work as you have them set now and should apply to all the control states.
Specifically, see the documentation for UIButton's titleLabel property: https://developer.apple.com/documentation/uikit/uibutton/1623992-titlelabel
titleLabel itself is read-only, but that doesn't mean you can't change the values of its own properties, such as font, line break mode, etc.
Here's how I worked it out using Swift 4.2.
counterButton.setTitle("Start Counter",
for:UIControl.State.normal)
I want to overlay an image over a button. the button has a background image. If i want to overlay a semi transparent image over the button, suppose i overlay a red color image, which is semi transparent. Can any one suggest how to do that? Suggest some documentation.
UIImageView *overlay = [[UIImageView alloc] initWithImage:...];
[overlay setAlpha:0.5];
UIButton *button = [[UIButton alloc] initWithType:UIButtonTypeRoundedRect];
[button setBackgroundImage:...];
[button addSubview:overlay];
[overlay release];
UIButton also has an image property as well as a backgroundImage property, although I'm unsure how transparency would work with it.
http://developer.apple.com/iphone/library/documentation/uikit/reference/UIButton_Class/UIButton/UIButton.html
Following dc answer, note that changing the button alpha will affect also the overlay transparency, so check that button alpha is 1.0f (default value).
Note also that you should release the overlay variable after adding it as subview to button, otherwise, you'll get a memory leak.
I have a UIToolbar that I've customized with my own background image. Consequently, the built-in UIBarButtonItem appearance doesn't work for me, so I'm using images that are already prepared to show in the bar. I create a custom button item with this method:
+ (UIBarButtonItem *)customWithImage:(UIImage *)image enabled:(BOOL)enabled target:(id)target action:(SEL)selector {
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
//I've tried different values for the frame here, but no luck
button.frame = CGRectMake(0, 0, 44, 44);
button.enabled = enabled;
button.showsTouchWhenHighlighted = YES;
[button addTarget:target action:selector forControlEvents:UIControlEventTouchUpInside];
[button setImage:image forState:UIControlStateNormal];
UIBarButtonItem *it = [[[UIBarButtonItem alloc] initWithCustomView:button] autorelease];
//Tried changing this, to no avail
it.width = 32.f;
return it;
I have one button on the left and one on the right and I'm trying to make it so that if you tap on the far left or far right of the UIToolbar, the corresponding button is tapped. However, with these custom buttons, the hit targets do not extend all the way to the edges of the UIToolbar, but are inset from the sides:
http://skitch.com/andpoul/d1p8g/hit-targets
Any help greatly appreciated!
UIBarButtonItem.width might be ignored if you're using a custom view (it probably just uses the width of the view).
A lame hack is to make the toolbar wider (so it sticks outside the screen) and add transparent edges to the background image to compensate. This brings the buttons closer to the edge of the screen.
An alternative is just to use a UIImageView with UIButton subviews.
I think the only way you will have to go is to make buttons wider (change image by adding some from left for one and right for another) and adjust size...