How can I make a button have a rounded border in Swift?
Use button.layer.cornerRadius
, button.layer.borderColor
and button.layer.borderWidth
.
Note that borderColor
requires a CGColor
, so you could say (Swift 3/4):
button.backgroundColor = .clear
button.layer.cornerRadius = 5
button.layer.borderWidth = 1
button.layer.borderColor = UIColor.black.cgColor
Button border with corner radius in Swift UI
Try it like this: Instead of setting the cornerRadius to the Button use an overlay for the inside View:
Edit: If you have a background for the button you also need to apply the cornerRadius to the background.
Button(action: {
print("sign up bin tapped")
}) {
Text("SIGN UP")
.frame(minWidth: 0, maxWidth: .infinity)
.font(.system(size: 18))
.padding()
.foregroundColor(.white)
.overlay(
RoundedRectangle(cornerRadius: 25)
.stroke(Color.white, lineWidth: 2)
)
}
.background(Color.yellow) // If you have this
.cornerRadius(25) // You also need the cornerRadius here
Works for me. Let me know if it helps!
How to round the corners of a button
I tried the following solution with the UITextArea and I expect this will work with UIButton as well.
First of all import this in your .m file -
#import <QuartzCore/QuartzCore.h>
and then in your loadView
method add following lines
yourButton.layer.cornerRadius = 10; // this value vary as per your desire
yourButton.clipsToBounds = YES;
Swift Left Side only rounded button with border
you can follow below steps to achieve the layout of Dropdown you want.
1.Use UITableView for dropdown.
2.Create a UITableViewCell.
3.Add Rounded Corner to the UIView on one side.
let rectShape = CAShapeLayer()
rectShape.bounds = self.roundedView.frame
rectShape.position = self.roundedView.center
rectShape.path = UIBezierPath(roundedRect: self.roundedView.bounds, byRoundingCorners: [.bottomLeft , .topLeft], cornerRadii: CGSize(width: 20, height: 20)).cgPath
self.roundedView.layer.backgroundColor = UIColor.purple.cgColor
//Here I'm masking the textView's layer with rectShape layer
self.roundedView.layer.mask = rectShape
4. Add border to roundedView.
How to make a simple rounded button in Storyboard?
To do it in the storyboard, you need to use an image for the button.
Alternatively you can do it in code:
btn.layer.cornerRadius = 10
btn.clipsToBounds = true
How change border color on round button (Swift)
Add your UIView
"containerView" as IBOutlet
and setup your containerView.layer.borderWidth = yourDesiredWidth
and setup the containerView.layer.borderColor = yourNeededColor.cgColor
Round Top Corners of a UIButton in Swift
Adding Extension of UIButton:
extension UIButton{
func roundedButton(){
let maskPath1 = UIBezierPath(roundedRect: bounds,
byRoundingCorners: [.topLeft , .topRight],
cornerRadii: CGSize(width: 8, height: 8))
let maskLayer1 = CAShapeLayer()
maskLayer1.frame = bounds
maskLayer1.path = maskPath1.cgPath
layer.mask = maskLayer1
}
}
Calling in viewDidAppear/viewDidLayoutSubviews:
btnCorner.roundedButton()
Button Corner OutPut:
How to make a rounded oval button?
Swift 3
myButton.layer.cornerRadius = myButton.frame.height / 2
myButton.backgroundColor = UIColor.blue
myButton.clipsToBounds = true
myButton.tintColor = UIColor.white
myButton.setTitle("Connect With Facebook", for: .normal)
Swift: Button round corner breaks contraints
You can get more reliable results by subclassing your button and placing your "rounding" code by overriding its layoutSubviews()
function.
First, if you want to add a border, you don't want to add multiple "border sublayers" ... so change your UIView
extension to this:
extension UIView {
func roundCorners(corners: UIRectCorner, radius: CGFloat, borderWidth: CGFloat?, borderColor: UIColor?) {
let maskPath = UIBezierPath(roundedRect: self.bounds, byRoundingCorners: corners, cornerRadii: CGSize(width: radius, height: radius))
let maskLayer = CAShapeLayer()
maskLayer.frame = self.bounds
maskLayer.path = maskPath.cgPath
self.layer.mask = maskLayer
self.layer.masksToBounds = true
if (borderWidth != nil && borderColor != nil) {
// remove previously added border layer
for layer in layer.sublayers! {
if layer.name == "borderLayer" {
layer.removeFromSuperlayer()
}
}
let borderLayer = CAShapeLayer()
borderLayer.frame = self.bounds;
borderLayer.path = maskPath.cgPath;
borderLayer.lineWidth = borderWidth ?? 0;
borderLayer.strokeColor = borderColor?.cgColor;
borderLayer.fillColor = UIColor.clear.cgColor;
borderLayer.name = "borderLayer"
self.layer.addSublayer(borderLayer);
}
}
}
Next, add a UIButton
subclass:
class RoundedButton: UIButton {
var corners: UIRectCorner?
var radius = CGFloat(0.0)
var borderWidth = CGFloat(0.0)
var borderColor: UIColor?
override func layoutSubviews() {
super.layoutSubviews()
// don't apply mask if corners is not set, or if radius is Zero
guard let _corners = corners, radius > 0.0 else {
return
}
roundCorners(corners: _corners, radius: radius, borderWidth: borderWidth, borderColor: borderColor)
}
}
This gives you a couple benefits: 1) It will update its mask layer frame when the button frame changes (rotating the device, for example), and 2) you could set these values either from your custom cell class or from cellForRowAt.
Either way, change your btnStatus
class from UIButton
to RoundedButton
- both in your storyboard and the @IBOutlet
connection.
Then change your CustomTableViewCell
to this:
class CustomTableViewCell: UITableViewCell {
@IBOutlet weak var btnStatus: RoundedButton!
override func awakeFromNib() {
super.awakeFromNib()
// set up corner maskign
btnStatus.corners = .bottomRight
btnStatus.radius = 7.0
// set if desired
// btnStatus.borderWidth = 2.0
// btnStatus.borderColor = .blue
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
}
And finally, your cellForRowAt
function becomes:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = self.tableView.dequeueReusableCell(withIdentifier: "customCell", for: indexPath) as! CustomTableViewCell
return cell
}
That should do it...
Related Topics
Read and Write Permission for User Selected Folder in MAC Os App
Slide Sidebar Menu iOS 8 Swift
Swift: How to Reload Row Height in Uitableviewcell Without Reloading Data
How to Identify Uppercase and Lowercase Characters in a String with Swift
Save Avcapturevideodataoutput to Movie File Using Avassetwriter in Swift
How to Debug "Precondition Failure" in Xcode
How to Make a Swift Enum with Associated Values Equatable
Gradient as Foreground Color of Text in Swiftui
Swift, How to Implement Hashable Protocol Based on Object Reference
Timestamped Event Matching Error: Failed to Find Matching Element
Differencebetween Final Class and Class
What Is Difference Between "" and "!" in Swift
Place Activity Indicator Over Uitable View
How to Build a Swift Package for iOS Over Command Line
In Swift, How to Write a Generic Function to Resize an Array
How to Detect and Make Clickable Links in a Uilabel Not Using Uitextview