How to make a simple rounded button in Storyboard?

后端 未结 13 1792
渐次进展
渐次进展 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 20:14

    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
    
    0 讨论(0)
  • 2021-01-29 20:15

    Short Answer: YES

    You can absolutely make a simple rounded button without the need of an additional background image or writing any code for the same. Just follow the screenshot given below, to set the runtime attributes for the button, to get the desired result.

    It won't show in the Storyboard but it will work fine when you run the project.

    Note:
    The 'Key Path' layer.cornerRadius and value is 5. The value needs to be changed according to the height and width of the button. The formula for it is the height of button * 0.50. So play around the value to see the expected rounded button in the simulator or on the physical device. This procedure will look tedious when you have more than one button to be rounded in the storyboard.

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

    I am using Xcode Version 11.4

    In the attribute inspector, you can define the corner radius.

    It won't show in the Storyboard but it will work fine when you run the project.

    0 讨论(0)
  • 2021-01-29 20:16
    1. Create a Cocoa Touch class.

    enter image description here

    1. Insert the code in RoundButton class.

      import UIKit
      
      @IBDesignable
      class RoundButton: UIButton {
      
          @IBInspectable var cornerRadius: CGFloat = 0{
              didSet{
              self.layer.cornerRadius = cornerRadius
              }
          }
      
          @IBInspectable var borderWidth: CGFloat = 0{
              didSet{
                  self.layer.borderWidth = borderWidth
              }
          }
      
          @IBInspectable var borderColor: UIColor = UIColor.clear{
              didSet{
                  self.layer.borderColor = borderColor.cgColor
              }
          }
      }
      
    2. Refer the image.

    enter image description here

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

    As other answer have suggested to perform most of this work in code only one answer actually provided a way to view your changes in the storyboard IB Interface. My answer goes beyond that answer by allowing you to change the cornerRadius of the view, button, image, etc.

    Please take a look at the following code. To use this code create a new swift file called RoundedView or whatever you would like to call it then go to your storyboard and change the class to either "RoundedView", "RoundedImageView" or "RoundedButton".

    import UIKit
    
    @IBDesignable class RoundedImage: UIImageView
    {
        override func layoutSubviews() {
            super.layoutSubviews()
    
            updateCornerRadius()
        }
    
        @IBInspectable var rounded: Bool = false {
            didSet {
                updateCornerRadius()
            }
        }
    
        @IBInspectable var cornerRadius: CGFloat = 0.1 {
            didSet {
                updateCornerRadius()
            }
        }
    
        func updateCornerRadius() {
            layer.cornerRadius = rounded ? cornerRadius : 0
            layer.masksToBounds = rounded ? true : false
        }
    }
    
    @IBDesignable class RoundedView: UIView
    {
        override func layoutSubviews() {
            super.layoutSubviews()
    
            updateCornerRadius()
        }
    
        @IBInspectable var rounded: Bool = false {
            didSet {
                updateCornerRadius()
            }
        }
    
        @IBInspectable var cornerRadius: CGFloat = 0.1 {
            didSet {
                updateCornerRadius()
            }
        }
    
        func updateCornerRadius() {
            layer.cornerRadius = rounded ? cornerRadius : 0
            layer.masksToBounds = rounded ? true : false
        }
    }
    
    @IBDesignable class RoundedButton: UIButton
    {
        override func layoutSubviews() {
            super.layoutSubviews()
    
            updateCornerRadius()
        }
    
        @IBInspectable var rounded: Bool = false {
            didSet {
                updateCornerRadius()
            }
        }
    
        @IBInspectable var cornerRadius: CGFloat = 0.1 {
            didSet {
                updateCornerRadius()
            }
        }
    
        func updateCornerRadius() {
            layer.cornerRadius = rounded ? cornerRadius : 0
            layer.masksToBounds = rounded ? true : false
        }
    }
    
    0 讨论(0)
  • 2021-01-29 20:18

    Follow the screenshot below. It works when you run the simulator (won't see it on preview)

    0 讨论(0)
提交回复
热议问题