How to make a simple rounded button in Storyboard?

后端 未结 13 1791
渐次进展
渐次进展 2021-01-29 19:49

I just started learning iOS development, cannot find how to make simple rounded button. I find resources for old versions. Do I need to set a custom background for a button? In

相关标签:
13条回答
  • 2021-01-29 19:54

    The extension is the best option for this problem. Create an extension of View or Button

    public extension UIView {
      //Round the corners
      func roundCorners(){
        let radius = bounds.maxX / 16
        layer.cornerRadius = radius
      }
    }
    

    Call it from the code

    button.roundCorners()
    
    0 讨论(0)
  • 2021-01-29 19:57

    While adding layer.cornerRadius in the storyboard make sure that you don't have leading or trailing spaces. If you do copy paste, you might get spaces inserted. Would be nice if XCode say some kind of warning or error.

    0 讨论(0)
  • 2021-01-29 20:00

    You can do something like this:

    @IBDesignable class MyButton: UIButton
    {
        override func layoutSubviews() {
            super.layoutSubviews()
    
            updateCornerRadius()
        }
    
        @IBInspectable var rounded: Bool = false {
            didSet {
                updateCornerRadius()
            }
        }
    
        func updateCornerRadius() {
            layer.cornerRadius = rounded ? frame.size.height / 2 : 0
        }
    }
    

    Set class to MyButton in Identity Inspector and in IB you will have rounded property:

    0 讨论(0)
  • 2021-01-29 20:00

    Try this!!

     override func viewDidLoad() {
       super.viewDidLoad()
    
       var button = UIButton.buttonWithType(.Custom) as UIButton
       button.frame = CGRectMake(160, 100, 200,40)
    
       button.layer.cornerRadius =5.0
       button.layer.borderColor = UIColor.redColor().CGColor
       button.layer.borderWidth = 2.0
    
       button.setImage(UIImage(named:"Placeholder.png"), forState: .Normal)
       button.addTarget(self, action: "OnClickroundButton", forControlEvents: .TouchUpInside)
       button.clipsToBounds = true
    
       view.addSubview(button)
    }
        func OnClickroundButton() {
       NSLog(@"roundButton Method Called");
    }
    
    0 讨论(0)
  • 2021-01-29 20:01

    You can connect IBOutlet of yur button from storyboard.

    Then you can set corner radius of your button to make it's corner round.

    for example, your outlet is myButton then,

    Obj - C

     self.myButton.layer.cornerRadius = 5.0 ;
    

    Swift

      myButton.layer.cornerRadius = 5.0
    

    If you want exact round button then your button's width and height must be equal and cornerRadius must be equal to height or width / 2 (half of the width or height).

    0 讨论(0)
  • 2021-01-29 20:02
    import UIKit
    
    @IBDesignable class MyButton: UIButton
    {
        override func layoutSubviews() {
            super.layoutSubviews()
    
    
        }
    
        func updateCornerRadius(radius:CGFloat) {
            layer.cornerRadius = radius
        }
        @IBInspectable var cornerRadius:CGFloat = 0{
            didSet{
                updateCornerRadius(radius: cornerRadius)
            }
        }
    }
    
    0 讨论(0)
提交回复
热议问题