View border Color not changing - swift

I have a view I make border with this run time attributes:
The problem is layer.borderColor when I set borderColor my border is gone but when I don't set border Color I have a black border which I don't want
any ideas?

You are facing this issue because layer.borderColor want CGColor and from User defined runtime attributes you can only set UIColor not CGColor, when you don't set the color it will take default borderColor and i.e black color. To set borderColor you need to set it programmatically like this.
Swift 3
yourView.layer.borderColor = UIColor.red.cgColor //set your color here
Swift 2.3 or lower
yourView.layer.borderColor = UIColor.redColor().CGColor //set your color here

In addition to what #NiravD suggested, you need to set both borderColor and the borderWidth on the view's layer to see the border.
Swift 3 & Swift 4:
circle.layer.cornerRadius = 5.0
circle.layer.borderColor = UIColor.red.cgColor
circle.layer.borderWidth = 2.0

in Swift 3.0
#IBOutlet weak var viewMainCell: UIView!
viewMainCell.layer.shadowOpacity = 0.5
viewMainCell.layer.shadowRadius = 5
viewMainCell.layer.shadowColor = UIColor.black.cgColor
viewMainCell.layer.cornerRadius = 5
viewMainCell.layer.masksToBounds = false

Related

UITextfield shadow

I want to add shadow to my UITextfield I tried code below, shadows work fine, but there is one thing, it is also set on the text inside my UITextfield any solutions ?
passwordTextField.layer.masksToBounds = false
passwordTextField.layer.shadowRadius = 4.0
passwordTextField.layer.shadowColor = UIColor.black.cgColor
passwordTextField.layer.shadowOffset = CGSize(width: 1.0, height: 1.0)
passwordTextField.layer.shadowOpacity = 1.0
picture for better understanding:
as you can see on the picture above, shadow is also set on textfield, how can I only set shadow to corner?
You should be able to just set the textfield background color to white and it will remove the text shadow.
passwordTextField.backgroundColor = .white // or whatever color you need

Can't set the same color for background and border in UISegmentedControl

I am trying to customize my segmented control with Swift, but I have run into a problem. I set the same color (e.g. UIColor.red) for border and background, but they look different. My code is:
segmentedControl.layer.cornerRadius = 12
segmentedControl.layer.borderWidth = 2
segmentedControl.layer.borderColor = UIColor.red.cgColor
segmentedControl.layer.masksToBounds = true
segmentedControl.backgroundColor = .red
Maybe someone knows how can I fix it?
Try setting the tintColor and selectedSegmentTintColor:
segmentedControl.tintColor = .white
segmentedControl.selectedSegmentTintColor = .white

UITextField border isn't clipping

I'm almost sure this question has been asked before, but I searched for almost an hour and didn't find anything
This is my code:
textField.font = UIFont.systemFont(ofSize: 12.0)
textField.layer.borderWidth = 0.9
textField.placeholder = "border is 0.9 clip is false"
textField.layer.borderColor = UIColor.black.cgColor
textField.borderStyle = UITextBorderStyle.roundedRect
textField.returnKeyType = UIReturnKeyType.done
Doesn't matter if set textField.clipsToBounds to true or false, the borders won't be rounded. However if I change the background color, it will fill it as it has a rounded border, see the image below.
if you zoom in, you'll see that the it doesn't fill the corners completely
If I set textField.layer.borderWidth = 0.1 then I would have a nice roundedRect, but they border would then have to be a thin line. See the image below:
again setting clipToBounds = true won't make any difference.
EDIT:
So the answer is to do textField.layer.cornerRadius = 5. But then I wonder what is the UITextBorderStyle.roundedRect doing then?!
Try textField.layer.cornerRadius = 5
cornerRadius - the radius to use when drawing rounded corners for the layer’s background. Animatable.
var cornerRadius: CGFloat { get set }
Setting the radius to a value greater than 0.0 causes the layer to
begin drawing rounded corners on its background. By default, the
corner radius does not apply to the image in the layer’s contents
property; it applies only to the background color and border of the
layer. However, setting the masksToBounds property to true causes the
content to be clipped to the rounded corners.
The default value of this property is 0.0.
Update:
But then I wonder what is the UITextBorderStyle.roundedRect doing then?
Basically the same thing but with default radius. Maybe it was overwritten somewhere. Check out your IB properties, in Attribute Inspector, Border Style property:

How to Convert a CAGradientLayer to a UIColor with Swift 3 or Hue?

I was trying to make a background color a gradient with Hue, (https://github.com/hyperoslo/Hue) and I got an error saying backgroundColor only works with UIColor. But would it be possible to change a CAGradientLayer to a UIColor. (I would prefer in the Hue framework, but it in apple frameworks is fine too.)
There is an example right on the hue page you linked to:
lazy var gradient: CAGradientLayer = [
UIColor(hex: "#FD4340"),
UIColor(hex: "#CE2BAE")
].gradient { gradient in
gradient.speed = 0
gradient.timeOffset = 0
return gradient
}
All you need to do is add it to your view as a sublayer.
view.layer.addSublayer(gradient)

Borders not covering background

I've got a UILabel is using a border the same color as a background which it is half obscuring, to create a nice visual effect. However the problem is that there is still a tiny, yet noticeable, sliver of the label's background color on the OUTSIDE of the border.
The border is not covering the whole label!
Changing the border width doesn't change anything either, sadly.
Here's a picture of what's going on, enlarged so you can see it:
And my code follows:
iconLbl.frame = CGRectMake(theWidth/2-20, bottomView.frame.minY-20, 40, 40)
iconLbl.font = UIFont.fontAwesomeOfSize(23)
iconLbl.text = String.fontAwesomeIconWithName(.Info)
iconLbl.layer.masksToBounds = true
iconLbl.layer.cornerRadius = iconLbl.frame.size.width/2
iconLbl.layer.borderWidth = 5
iconLbl.layer.borderColor = topBackgroundColor.CGColor
iconLbl.backgroundColor = UIColor.cyanColor()
iconLbl.textColor = UIColor.whiteColor()
Is there something I'm missing?
Or am I going to have to figure out another to achieve this effect?
Thanks!
EDIT:
List of things I've tried so far!
Changing layer.borderWidth
Fussing around with clipsToBounds/MasksToBounds
Playing around the the layer.frame
Playing around with an integral frame
EDIT 2:
No fix was found! I used a workaround by extending this method on to my UIViewController
func makeFakeBorder(inputView:UIView,width:CGFloat,color:UIColor) -> UIView {
let fakeBorder = UIView()
fakeBorder.frame = CGRectMake(inputView.frame.origin.x-width, inputView.frame.origin.y-width, inputView.frame.size.width+width*2, inputView.frame.size.height+width*2)
fakeBorder.backgroundColor = color
fakeBorder.clipsToBounds = true
fakeBorder.layer.cornerRadius = fakeBorder.frame.size.width/2
fakeBorder.addSubview(inputView)
inputView.center = CGPointMake(fakeBorder.frame.size.width/2, fakeBorder.frame.size.height/2)
return fakeBorder
}
I believe this is the way a border is drawn to a layer in iOS. In the document it says:
When this value is greater than 0.0, the layer draws a border using the current borderColor value. The border is drawn inset from the receiver’s bounds by the value specified in this property. It is composited above the receiver’s contents and sublayers and includes the effects of the cornerRadius property.
One way to fix this is to apply a mask to a view's layer, but I found out that even if so we still can see a teeny tiny line around the view when doing snapshot tests. So to fix it more, I put this code to layoutSubviews
class MyView: UIView {
override func layoutSubviews() {
super.layoutSubviews()
let maskInset: CGFloat = 1
// Extends the layer's frame.
layer.frame = layer.frame.inset(dx: -maskInset, dy: -maskInset)
// Increase the border width
layer.borderWidth = layer.borderWidth + maskInset
layer.cornerRadius = bounds.height / 2
layer.maskToBounds = true
// Create a circle shape layer with true bounds.
let mask = CAShapeLayer()
mask.path = UIBezierPath(ovalIn: bounds.inset(dx: maskInset, dy: maskInset)).cgPath
layer.mask = mask
}
}
CALayer's mask