Can't set CALayer's border color
The problem is that you are running this code too soon (in viewDidLoad
). Your cornerRadius
calculation depends upon calculateButton.frame
, but its value is not known at this time. Move your code into viewDidLayoutSubviews
and I think you will find that it works as you expect.
CALayer: add a border only at one side
I made a right border using this:
leftScrollView.clipsToBounds = YES;
CALayer *rightBorder = [CALayer layer];
rightBorder.borderColor = [UIColor darkGrayColor].CGColor;
rightBorder.borderWidth = 1;
rightBorder.frame = CGRectMake(-1, -1, CGRectGetWidth(leftScrollView.frame), CGRectGetHeight(leftScrollView.frame)+2);
[leftScrollView.layer addSublayer:rightBorder];
How to make CALayer border alpha value different with the bgcolor
Can I make the alpha of the border different with the bgcolor?
Yes. You could do something like this:
CALayer * firstLayer = [[CALayer alloc] init];
[firstLayer setFrame:CGRectMake(100, 100, 100, 100)];
firstLayer.borderColor = [UIColor colorWithRed:1.0 green:1.0 blue:1.0 alpha:1.0].CGColor;
firstLayer.borderWidth = 3.0f;
firstLayer.backgroundColor = [UIColor colorWithRed:1.0 green:1.0 blue:1.0 alpha:0.5].CGColor;
Another approach would be to have a layer inside another layer of the same size. Give the outer layer the border thickness you want and make sure it's opacity is 100%. The second layer should have the fractional opacity:
CALayer * firstLayer = [[CALayer alloc] init];
CALayer * secondLayer = [[CALayer alloc] init];
[firstLayer setFrame:CGRectMake(100, 100, 100, 100)];
[secondLayer setFrame:CGRectMake(0, 0, 100, 100)];
[firstLayer addSublayer:secondLayer];
firstLayer.borderColor = [UIColor redColor].CGColor;
firstLayer.borderWidth = 1.0f;
secondLayer.backgroundColor = [UIColor redColor].CGColor;
secondLayer.opacity = 0.8f;
[self.view.layer addSublayer:firstLayer];
UIView's border color in Interface builder doesn't work?
It's possible to do this, but it's not a built-in feature. This is because the Color
type in the User Defined Runtime Attributes panel creates a UIColor
, but layer.borderColor
holds a CGColorRef
type. Unfortunately, there's no way to assign a CGColorRef
type in Interface Builder.
However, this is possible through a proxy property. See Peter DeWeese's answer to a different question for a possible solution to this problem. His answer defines a category that allows a proxy color to be set through Interface Builder.
How to add a border just on the top side of a UIView
I consider subclassing UIView
and overriding drawRect
overkill here. Why not add an extension on UIView
and add border subviews?
@discardableResult
func addBorders(edges: UIRectEdge,
color: UIColor,
inset: CGFloat = 0.0,
thickness: CGFloat = 1.0) -> [UIView] {
var borders = [UIView]()
@discardableResult
func addBorder(formats: String...) -> UIView {
let border = UIView(frame: .zero)
border.backgroundColor = color
border.translatesAutoresizingMaskIntoConstraints = false
addSubview(border)
addConstraints(formats.flatMap {
NSLayoutConstraint.constraints(withVisualFormat: $0,
options: [],
metrics: ["inset": inset, "thickness": thickness],
views: ["border": border]) })
borders.append(border)
return border
}
if edges.contains(.top) || edges.contains(.all) {
addBorder(formats: "V:|-0-[border(==thickness)]", "H:|-inset-[border]-inset-|")
}
if edges.contains(.bottom) || edges.contains(.all) {
addBorder(formats: "V:[border(==thickness)]-0-|", "H:|-inset-[border]-inset-|")
}
if edges.contains(.left) || edges.contains(.all) {
addBorder(formats: "V:|-inset-[border]-inset-|", "H:|-0-[border(==thickness)]")
}
if edges.contains(.right) || edges.contains(.all) {
addBorder(formats: "V:|-inset-[border]-inset-|", "H:[border(==thickness)]-0-|")
}
return borders
}
// Usage:
view.addBorder(edges: [.all]) // All with default arguments
view.addBorder(edges: [.top], color: .green) // Just Top, green, default thickness
view.addBorder(edges: [.left, .right, .bottom], color: .red, thickness: 3) // All except Top, red, thickness 3
With this code you're not tied to your subclass too, you can apply it to anything and everything that inherits from UIView
- reusable in your project, and any others. Pass in other arguments to your methods to define other colours and widths. Many options.
Add a border outside of a UIView (instead of inside)
Unfortunately, there isn't simply a little property you can set to align the border to the outside. It draws aligned to the inside because the UIViews default drawing operations draw within its bounds.
The simplest solution that comes to mind would be to expand the UIView by the size of the border width when applying the border:
CGFloat borderWidth = 2.0f;
self.frame = CGRectInset(self.frame, -borderWidth, -borderWidth);
self.layer.borderColor = [UIColor yellowColor].CGColor;
self.layer.borderWidth = borderWidth;
Runtime attributes border color
Actually, you are using wrong attribute.The, correct attribute is layer.borderColor
.
But again it will not work because it is type of CGColor and from IB we can only assign UIColor, we can't assign CGColor.
Eihter you can simply do it programaticlly.
Or
You can create extension with type CGColor.
Related Topics
How to Create an Alert in a Swift File Model That Works for Various View Controller
Xcode 9: Swift Dependency Analysis Error
Non-Main Bundle File as Alert Sound
Countdown with Several Decimal Slots, Using Nstimer in Swift
How to Pass Values from a Pop Up View Controller to the Previous View Controller
Nsfilemanager Moveitem Throws Error Code=513
Dyld: Library Not Loaded: @Rpath/Mydsk.Framework/Mydsk -> Swift iOS 8.0
How to Change Pin Color in Mapkit Under the Same Annotationview (Swift3)
Scroll All the Way Down to the Bottom of Uitableview
Creating a Rtsp Client for Live Audio and Video Broadcasting in Objective C
Allow Users to Send Messages to Multiple Users Simultaneously in a Messaging App
Why Does It Take Such a Long Time for UI to Be Updated from Background Thread
Rxswift - Generic Parameter 'Self' Could Not Be Inferred
How to Set the Collection View Cell Size Exactly Equal to the Collection View in iOS