i have over 20 buttons in my XCode project, and i want to add border to every of them. is it possible to like select all the UIButtons in the storyboard and then perform codes on them?
// For example
buttons.borderColor = [[UIColor darkGrayColor] CGColor];
buttons.cornerRadius = 8;
buttons.borderWidth = 1;
You could use the UIAppearance protocol.
https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIAppearance_Protocol/index.html
I have custom UICollectionViewCell and I have placed 2 UILabels in it. Now I want to change the borderRadius and borderWidth of the labels in it. Can anyone tell me how to do this?
I am doing in this way, but nothing is happening:
self.titleLabel.layer.borderWidth = 2.0;
self.titleLabel.layer.borderColor = [UIColor blackColor].CGColor;
I am putting these two lines in initWithFrame method.
If you want to set the borderWidth of UILabel inside means why you are using
self.titleLabel.layer.borderWidth = 2.0;
Instead of this you can directly use the label name like,
yourLabel1.layer.borderWidth = 2.0;
yourLabel2.layer.borderWidth = 2.0;
Like wise you can change the borderRadius also.
With the following line added the border should appear:
self.titleLabel.layer.masksToBounds = YES;
I want to make my login interface a little bit more fancy. So I wanted to build somethin like this:
Thats what i Have to far:
self.usernameTextField.layer.borderWidth = 1.0f;
self.usernameTextField.layer.borderColor = [[UIColor colorWithRed:171.0/255.0 green:171.0/255.0 blue:171.0/255.0 alpha:1.0] CGColor];
self.usernameTextField.layer.cornerRadius = 5;
self.usernameTextField.clipsToBounds = YES;
self.passwordTextField.layer.borderWidth = 2.0f;
self.passwordTextField.layer.borderColor = [[UIColor colorWithRed:171.0/255.0 green:171.0/255.0 blue:171.0/255.0 alpha:1.0] CGColor];
self.passwordTextField.layer.cornerRadius = 5;
self.passwordTextField.clipsToBounds = YES;
How can I connect them? There is still a gap between the two UITextFields..
Put plain image background like your interface and put two text fields on that image and set boarder style like below.
[yourTextField setBorderStyle:UITextBorderStyleNone];
The example you show is almost certainly using a table view. If you want to mimic it created a grouped table view with 2 static rows and add your text fields to each row. Make sure the text fields border style is set to none (it should be the default in IB), then set your placeholder text and color. If you want the icons, that is probably just a small UIImageView to the left of the text field.
You can set your UITextField border style to none.
[self.textField setBorderStyle:UITextBorderStyleNone];
and then use custom background images, to get the desired look.
Create your both TextField custom type. Dont set any border style.. Fix the image in ImageView and then place the two text field frame for the text in imageview.
According to the documentation for UIVIew #property(nonatomic) CGFloat alpha
The value of this property is a floating-point number in the range 0.0
to 1.0, where 0.0 represents totally transparent and 1.0 represents
totally opaque.
This value affects only the current view and does not affect any of its embedded subviews.
I have a container view configured as follows:
self.myView.backgroundColor = [UIColor blackColor];
self.myView.alpha = 0.5;
[self addSubview:self.myView];
And then add subviews to 'myView'
[myView addSubView anotherView];
anotherView.alpha = 1;
NSLog(#"anotherView alpha = %f",anotherView.alpha); // prints 1.0000 as expected
But 'anotherView' does have alpha on screen (it is not opaque as expected)
How can this be and what can be done?
I think this is a bug in the documentation. You should file it at bugreport.apple.com.
Everything I can see after a bit of quick research suggests what you are seeing is how it always has behaved, and my own testing shows it too.
The alpha of a view is applied to all subviews.
Perhaps all you need is [[UIColor blackColor] colorWithAlphaComponent:0.5] but if not you will need to make the view a sibling instead of a child.
Don't set the alpha directly on the parent view. Instead of it use the below line of code which will apply transparency to parentview without affecting its child views.
[parentView setBackgroundColor:[[UIColor clearColor] colorWithAlphaComponent:0.5]];
In swift
view.backgroundColor = UIColor.whiteColor().colorWithAlphaComponent(0.5)
UPDATED FOR SWIFT 3
view.backgroundColor = UIColor.white.withAlphaComponent(0.5)
Set Opacity of the background color instead of alpha will not affect its child views.
select view.
go to attribute inspector than background color
click on "others"
set opacity to 30%
Or you can set by programmetically
var customView:UIView = UIView()
customView.layer.opacity = 0.3
Thats it. Happy Coding!!!
If you like Storyboards, put a User Defined Runtime Attribute for your view in the Identity Inspector:
Key Path: backgroundColor, Type: Color, Value: e.g. white color with Opacity 50 %.
Simplest solution as discussed is to change the alpha as follows :
Updated version for Xcode 8 Swift 3 is :
yourParentView.backgroundColor = UIColor.black.withAlphaComponent(0.4)
Objective C:
yourParentView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.5];
Refer Apple Developer Docs here :
https://developer.apple.com/reference/uikit/uiview/1622417-alpha
In Swift 4.2 and Xcode 10.1
Don't add colour and alpha value through storyboard. Only programmatic approach will work in this case.
transparentView.backgroundColor = UIColor.black.withAlphaComponent(0.5)
Here is a bit complex solution:
UIView *container;
UIView *myView;
UIView *anotherView;
myView.alpha = 0.5;
[container addSubview:myView];
anotherView.alpha = 1;
[container addSubview:anotherView];
Use a container view as superview, anotherView and myView are both subview in container, anotherView is not a subview in myView.
For now there is only one way make the Parent View transparent and don't put any child views inside (don't put any views as subview) the parent view, put that child views outside of the parent view. To make parent view transparent you can do this via storyboard.
//Transparent the parentView
parentView.backgroundColor = UIColor(red: 0, green: 0, blue: 0, alpha: 0.8)
Put the other view outside of the parent view. It will work like a charm.
Please refer to the bold description from Xcode documentation.
The value of this property is a floating-point number in the range 0.0 to 1.0, where 0.0 represents totally transparent and 1.0 represents totally opaque. Changing the value of this property updates the alpha value of the current view only. However, the transparency imparted by that alpha value affects all of the view's contents, including its subviews. For example, a subview with an alpha value of 1.0 that is embedded in a parent view with an alpha value of 0.5, appears onscreen as if its alpha value is also 0.5.
I've seen lots of snippets that either: are too complicated for something as simple as a drop shadow, requiring subclassing UIView and using quartz2d calls, or I can't get them to work.
I just want to do this on a view I'm adding as a subview to another one (the subview is taken from another viewController I'm just allocating - I know that's probably not nice but oh well), no IB or anything. what's the simplest / most accepted way to go about it? would it be different if I want it to work on iOS 4?
It's as easy as importing <QuartzCore/QuartzCore.h> and using a similar snippet as below:
self.viewAboutContainer.layer.shadowColor = [[UIColor blackColor] CGColor];
self.viewAboutContainer.layer.shadowOpacity = 0.7;
self.viewAboutContainer.layer.shadowRadius = 4.0;
self.viewAboutContainer.layer.shadowOffset = CGSizeMake(5.0f, 5.0f);
self.viewAboutContainer.layer.shadowPath = [UIBezierPath bezierPathWithRect:self.viewAboutContainer.bounds].CGPath;