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.
UIView with rounded corners and drop shadow?
The following code snippet adds a border, border radius, and drop shadow to v
, a UIView
:
// border radius
[v.layer setCornerRadius:30.0f];
// border
[v.layer setBorderColor:[UIColor lightGrayColor].CGColor];
[v.layer setBorderWidth:1.5f];
// drop shadow
[v.layer setShadowColor:[UIColor blackColor].CGColor];
[v.layer setShadowOpacity:0.8];
[v.layer setShadowRadius:3.0];
[v.layer setShadowOffset:CGSizeMake(2.0, 2.0)];
Swift 5 Version :
// border radius
v.layer.cornerRadius = 30.0
// border
v.layer.borderColor = UIColor.lightGray.cgColor
v.layer.borderWidth = 1.5
// drop shadow
v.layer.shadowColor = UIColor.black.cgColor
v.layer.shadowOpacity = 0.8
v.layer.shadowRadius = 3.0
v.layer.shadowOffset = CGSize(width: 2.0, height: 2.0)
You can adjust the settings to suit your needs.
Also, add the QuartzCore framework to your project and:
#import <QuartzCore/QuartzCore.h>
See my other answer regarding masksToBounds
.
Note
This may not work in all cases. If you find that this method interferes with other drawing operations that you are performing, please see this answer.
How to add corner radius for UIView
You shouldn't use a mask for this, you can simply use the layer.maskedCorners
property.
layer.cornerRadius = r
layer.maskedCorners = [.layerMinXMinYCorner, .layerMaxXMinYCorner]
Use Storyboard to mask UIView and give rounded corners?
Yes, I use that a lot but question like this was already answered many times.
But anyway in Interface Builder.
You need to add User Defined Runtime Attributes like this:
layer.masksToBounds Boolean YES
layer.cornerRadius Number {View's Width/2}
and enable
Clips subviews
Results:
Rounded corners changes UIView size
Its because your CAShapeLayer is not automatically resizing once the Header View's constraints are being resized. You need to update the path whenever the view is resized:
let headerView: UIView!
override func awakeFromNib() {
super.awakeFromNib()
headerView = UIView(frame: bounds)
headerView.frame = bounds
headerView.leadingAnchor.constraint(equalTo: self.leadingAnchor, constant: 8).isActive = true
headerView.topAnchor.constraint(equalTo: self.topAnchor, constant: 4).isActive = true
headerView.trailingAnchor.constraint(equalTo: self.trailingAnchor, constant: -8).isActive = true
headerView.heightAnchor.constraint(equalToConstant: 40).isActive = true
let rectShape = CAShapeLayer()
rectShape.bounds = headerView.layer.frame
rectShape.position = headerView.center
headerView.layer.backgroundColor = UIColor.green.cgColor
headerView.layer.mask = rectShape
}
override func layoutSubviews() {
super.layoutSubviews()
rectShape.path = UIBezierPath(roundedRect: headerView.layer.bounds, byRoundingCorners: [ .topLeft, .topRight], cornerRadii: CGSize(width: 10, height: 10)).cgPath
}
I used awakeFromNib just as an example. Your setup for your view may be different in your custom UITableViewCell class.
Setting corner radius hides UIView
It seems, that you need use self.bounds instead self.frame in UIBezierPath(roundedRect: self.frame, byRoundingCorners: corners, cornerRadii: CGSize(width: radius, height: 0.0))
How to control an UIView's rounded corners separately using @IBInspectable in Swift 3?
Use this custom class, basically you need create a bezier path, using lines and quad curves, handling every corner with values in @IBInspectable
properties.
This is the code
//
// CornerView.swift
// UIViewCornerRounded
//
// Created by Reinier Melian on 21/07/2017.
// Copyright © 2017 Pruebas. All rights reserved.
//
import UIKit
@IBDesignable
class CornerView: UIView {
@IBInspectable var leftTopRadius : CGFloat = 0{
didSet{
self.applyMask()
}
}
@IBInspectable var rightTopRadius : CGFloat = 0{
didSet{
self.applyMask()
}
}
@IBInspectable var rightBottomRadius : CGFloat = 0{
didSet{
self.applyMask()
}
}
@IBInspectable var leftBottomRadius : CGFloat = 0{
didSet{
self.applyMask()
}
}
override func layoutSubviews() {
super.layoutSubviews()
self.applyMask()
}
// Only override draw() if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
/*override func draw(_ rect: CGRect) {
super.draw(rect)
}*/
func applyMask()
{
let shapeLayer = CAShapeLayer(layer: self.layer)
shapeLayer.path = self.pathForCornersRounded(rect:self.bounds).cgPath
shapeLayer.frame = self.bounds
shapeLayer.masksToBounds = true
self.layer.mask = shapeLayer
}
func pathForCornersRounded(rect:CGRect) ->UIBezierPath
{
let path = UIBezierPath()
path.move(to: CGPoint(x: 0 + leftTopRadius , y: 0))
path.addLine(to: CGPoint(x: rect.size.width - rightTopRadius , y: 0))
path.addQuadCurve(to: CGPoint(x: rect.size.width , y: rightTopRadius), controlPoint: CGPoint(x: rect.size.width, y: 0))
path.addLine(to: CGPoint(x: rect.size.width , y: rect.size.height - rightBottomRadius))
path.addQuadCurve(to: CGPoint(x: rect.size.width - rightBottomRadius , y: rect.size.height), controlPoint: CGPoint(x: rect.size.width, y: rect.size.height))
path.addLine(to: CGPoint(x: leftBottomRadius , y: rect.size.height))
path.addQuadCurve(to: CGPoint(x: 0 , y: rect.size.height - leftBottomRadius), controlPoint: CGPoint(x: 0, y: rect.size.height))
path.addLine(to: CGPoint(x: 0 , y: leftTopRadius))
path.addQuadCurve(to: CGPoint(x: 0 + leftTopRadius , y: 0), controlPoint: CGPoint(x: 0, y: 0))
path.close()
return path
}
}
Here is the results
Using this values
Hope this helps
Swift UIView Subviews not rounding corners in Custom UIView Subclass
An easy way to make it look as expected is to add layoutIfNeeded()
call inside your makeCircle(v:UIView)
method. This will make you sure that all views' frames are updated correctly before applying visual changes:
func makeCircle(v:UIView) {
v.layoutIfNeeded()
v.layer.cornerRadius = v.frame.size.width * 0.50
v.clipsToBounds = true
}
Related Topics
Controlling the Screenshot in the iOS 7 Multitasking Switcher
Ios: Convert Utc Nsdate to Local Timezone
Prevent Screen Capture in an iOS App
How to Detect Avplayer and Get Url of Current Video from Wkwebview
Converting Storyboard from Iphone to Ipad
Best Practices For Storyboard Login Screen, Handling Clearing of Data Upon Logout
Request Failed: Unacceptable Content-Type: Text/Html Using Afnetworking 2.0
Move Textfield Up When the Keyboard Has Appeared in Swiftui
Uitapgesturerecognizer - Make It Work on Touch Down, Not Touch Up
First and Last Day of the Current Month in Swift
How to Write a File to a Folder Located At Apple'S Files App in Swift
Iphone Navigation Bar Title Text Color
Multiple Lines of Text in Uilabel
Creating a Uicollectionview Programmatically
How to Create a Button Programmatically
Xcode 10.2.1 Command Phasescriptexecution Failed With a Nonzero Exit Code