So right now I have a UIView with a UILabel in it. I want the background to have an opacity < 1.0 and the label to have an opacity of 1.0. However since alphas propagate down the view hierarchy, the label ends up with an opacity < 1.0 as well.
Is there anyway to do what I want without making the UILabel a subview of another view??
Just set the background color to be semitransparent:
view.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.5f];
Or, in Swift:
view.backgroundColor = UIColor.blackColor().colorWithAlphaComponent(0.5)
Or, Swift 3:
view.backgroundColor = UIColor.black.withAlphaComponent(0.5)
Note that, in this particular case, UIColor(white: 0, alpha: 0.5) is more concise, but colorWithAlphaComponent will work in general.
Besides being available in code, you can do this quite easily from iB as well:
Within the storyboard, select the view you wish to edit;
From the right panel, make sure the Attributes inspector is opened;
Click on the right side of the "Background" drop down box and choose "Other ..."; it will open a colour picker dialog;
Change the "Opacity" at the bottom to set the background colour opacity.
You can set the background color of the UIView with a semi-transparent color or make the image itself semi-transparent. This way it's a property of the view that is transparent, not the view itself.
You can use this:
self.view.layer.opacity=0.5
Related
So I have an NSView in which I created some borderless buttons programmatically.
Although I set their backgroundColor property to desired color, they show up like they have a darker shade.
I guess that is the background of Label, not of Button, which does not appear when users choose to reduce transparency in System Preferences.
In that case, I have solved by this line of code:
myButton.appearance = NSAppearance(named: .aqua)
here How I would do it as I mentioned in comment.
The custom view is connected as iboutlet and given green color.
Label has by default no back ground color.
The button has no border and of type Round Rect as you see in screen shot.
The blue color around button is the selection/ active control, but you can get rid of that too.
self.testBtn.focusRingType = NSFocusRingTypeNone;
edit add new image showing scroll bar:
//set background color
button.backgroundColor=[UIColor clearColor]
//then set tint color of button
[btn setTintColor:[UIColor clearColor]];
I know I can create border using below code.
[[myImageView layer] setBorderWidth:2.0f];
[[myImageView layer] setBorderColor:[UIColor greenColor].CGColor];
However this draw border inside image.
What I was looking is draw border outside ImageView.
Note:
I search online for this and found below.
Can be done by using another image which will have border.
Can be done by drawing another view which is little bigger then current image.
Is there quick way (especially in-built in iOS), where I can draw border outside UIImageView? Any views?
Why don't you try border with using imageview's frame ?
CGFloat borderWidth = 5.0f;
self.imgview.frame = CGRectInset(self.imgview. frame, -borderWidth, -borderWidth);
self.imgview. layer.borderColor = [UIColor blueColor].CGColor;
self.imgview. layer.borderWidth = borderWidth;
There is no quickway in-built in iOS, there is no margin that you could set on the image layer.
If I were you, I'd develop a new class that inherit from UIView (ex UIImageWithBorderView) and which include a UIImageView and making the "UIImageWithBorderView" bigger than the UIImageView (and think about NOT to autoresize the UIImageView with the UIView parent, otherwise your UIImageView will be stretched, and prevent the UIImageWithBorderView from being smaller than the UIImageView frame), and then add borders to the "UIImageWithBorderView".
This way, your UIImageView will be intact and you'll have a specific, reusable composant for your needs.
Hope it helps !
I have a problem. I am trying to make my custom UIView with an image in the very center. The part outside of that image got to be transparent and grayed.
In xib for "View" I've set this:
The result - fail. The region outside the image is just gray, not transparent.
But if I add
-(void) viewDidLoad
{
self.view.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.5f];
}
Then everything works as expected.
What was I doing wrong when trying to achieve transparent backround in xib?
UPDATE:
I found the code which was doing presenting of the view, and there was alpha set inside of it. So I guess that was the problem. Thanks. Sorry for being so newbie
Maybe this one is what you expected. You can only change the background Color rather the view's alpha. Just set opacity with the background Color.
When setting the alpha of a parent UIView, the alpha value will be passed to the child views, giving the same alpha to all the child views. If you set the alpha value of the UIView to 1, you are setting the alpha of the view, not the color.
That said, you can set the alpha of the background color. When you do this, the view maintains its own alpha value, while the background color has its own alpha value. Hope this helps
The issue was in custom show method. It didn't use presentViewController:animated:completion: method. It used alpha animaiton from 0 to 1. The second - when using standard presentViewController:animated:completion: method cocoa optimization was hiding the underlying view, so I need to set up (for iOS 8)
vc_to_present_on.modalPresentationStyle = UIModalPresentationCurrentContext;
vc_to_be_presented.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
And then everything work as expected
I have a probem like this. We know we can make transparent an UIView by changing Alpha value of that view. But I dont want to make transparent buttons which are inside that view. My problem is when I set the parent view alpha, button also get transparent and button title not visible clearly. How can I overcome this problem.
Set the background color of the UIView with variable alpha, like this:
view.backgroundColor = [[UIColor white] colorWithAlphaComponent:0.5f];
How can add a light gray shadow to a UIButton, I don't want a method to do this at the moment, it should be something like:
UIButton *button1... button1.layer.shadowOpacity = 0.8
etc, but that doesn't work, it only adds a shadow inside the button, but I need it on the outside. Thanks!
First you have to #import <QuartzCore/QuartzCore.h>. Then:
mybtn.layer.shadowColor = [UIColor blackColor].CGColor;
mybtn.layer.shadowOpacity = 0.5;
mybtn.layer.shadowRadius = 2;
mybtn.layer.shadowOffset = CGSizeMake(3.0f,3.0f);
You can also use –[UIButton setBackgroundImage:forState:] to set the background image for UIControlStateNormal to one with a shadow. E.g.:
[button setBackgroundImage:[UIImage imageNamed:#"ButtonBackgroundNormal"]
forState:UIControlStateNormal];
where ButtonBackgroundNormal.png has a shadow. Images often render faster than drawing with code. And, speed is important, especially if you're adding it to a UITableViewCell. In that case, to speed up scrolling speed, make sure the background image is completely opaque by designing it with the same background color of the UITableViewCell and saving it without transparency. Then, set button.opaque = YES.