Changing the Shape of a Uiview Border

Changing the shape of a UIView border

The easiest approach would be to use a UIImageView. However, it is also possible by creating a custom border for the UIView but that will require a lot of code to draw the shapes.

How to change border color of UIview?

Don't use UITapGestureRecognizer. You can use UILongPressGestureRecognizer

Code:

   override func viewDidLoad() {
super.viewDidLoad()

let view = UIView.init(frame: CGRect.init(x: 30, y: 200, width: 100, height: 40))
self.view.addSubview(view)
view.layer.borderColor = UIColor.black.cgColor
view.layer.borderWidth = 3
let tapForView = UILongPressGestureRecognizer(target: self, action: #selector(self.toChangeColor(recognizer:)))
tapForView.minimumPressDuration = 0.01
view.isUserInteractionEnabled = true
view.addGestureRecognizer(tapForView)
}

@objc func toChangeColor(recognizer:UILongPressGestureRecognizer)
{
// Apply logic for changing background color.
let view = recognizer.view
if recognizer.state == .began {
view?.layer.borderColor = UIColor.orange.cgColor
print("view began")
}
else if recognizer.state == .ended {
view?.layer.borderColor = UIColor.black.cgColor
print("view ended")

}

}

This will work perfectly.

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;

How to set cornerRadius for only top-left and top-right corner of a UIView?

Pay attention to the fact that if you have layout constraints attached to it, you must refresh this as follows in your UIView subclass:

override func layoutSubviews() {
super.layoutSubviews()
roundCorners(corners: [.topLeft, .topRight], radius: 3.0)
}

If you don't do that it won't show up.


And to round corners, use the extension:

extension UIView {
func roundCorners(corners: UIRectCorner, radius: CGFloat) {
let path = UIBezierPath(roundedRect: bounds, byRoundingCorners: corners, cornerRadii: CGSize(width: radius, height: radius))
let mask = CAShapeLayer()
mask.path = path.cgPath
layer.mask = mask
}
}



Additional view controller case: Whether you can't or wouldn't want to subclass a view, you can still round a view. Do it from its view controller by overriding the viewWillLayoutSubviews() function, as follows:

class MyVC: UIViewController {
/// The view to round the top-left and top-right hand corners
let theView: UIView = {
let v = UIView(frame: CGRect(x: 10, y: 10, width: 200, height: 200))
v.backgroundColor = .red
return v
}()

override func loadView() {
super.loadView()
view.addSubview(theView)
}

override func viewWillLayoutSubviews() {
super.viewWillLayoutSubviews()

// Call the roundCorners() func right there.
theView.roundCorners(corners: [.topLeft, .topRight], radius: 30)
}
}

Giving UIView rounded corners

Try this

#import <QuartzCore/QuartzCore.h> // not necessary for 10 years now  :)

...

view.layer.cornerRadius = 5;
view.layer.masksToBounds = true;

Note: If you are trying to apply rounded corners to a UIViewController's view, it should not be applied in the view controller's constructor, but rather in -viewDidLoad, after view is actually instantiated.

how to set a border color to a masked UIView swift

You already use CAShapeLayer(), so you no need too add view with border. just custom only CAShapeLayer() it's enough.

shapeLayer.lineWidth = 2.0 

try this with your CAShapeLayer()



Related Topics



Leave a reply



Submit