How to round the corners of a button

前端 未结 15 642
隐瞒了意图╮
隐瞒了意图╮ 2020-12-07 06:54

I have a rectangle image (jpg) and want to use it to fill the background of a button with rounded corner in xcode.

I wrote the following:

UIButton *b         


        
相关标签:
15条回答
  • 2020-12-07 07:24

    Try my code. Here you can set all properties of UIButton like text colour, background colour, corner radius, etc.

    extension UIButton {
        func btnCorner() {
            layer.cornerRadius = 10
            clipsToBounds = true
            backgroundColor = .blue
        }
    }
    

    Now call like this

    yourBtnName.btnCorner()
    
    0 讨论(0)
  • 2020-12-07 07:24

    If you want a rounded corner only to one corner or two corners, etc... read this post:

    [ObjC] – UIButton with rounded corner - http://goo.gl/kfzvKP

    It's a XIB/Storyboard subclass. Import and set borders without write code.

    0 讨论(0)
  • 2020-12-07 07:25

    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;
    
    0 讨论(0)
  • 2020-12-07 07:25

    Swift 4 Update

    I also tried many options still i wasn't able to get my UIButton round cornered. I added the corner radius code inside the viewDidLayoutSubviews() Solved My issue.

    func viewDidLayoutSubviews() {
            super.viewDidLayoutSubviews()
            anyButton.layer.cornerRadius = anyButton.frame.height / 2
        }
    

    Also we can adjust the cornerRadius as follows

    func viewDidLayoutSubviews() {
            super.viewDidLayoutSubviews()
            anyButton.layer.cornerRadius = 10 //Any suitable number as you prefer can be applied 
        }
    
    0 讨论(0)
  • 2020-12-07 07:27

    For Objective C:

    submitButton.layer.cornerRadius = 5;
    submitButton.clipsToBounds = YES;
    

    For Swift:

    submitButton.layer.cornerRadius = 5
    submitButton.clipsToBounds = true
    
    0 讨论(0)
  • 2020-12-07 07:29

    For iOS SWift 4

    button.layer.cornerRadius = 25;
    button.layer.masksToBounds = true;
    
    0 讨论(0)
提交回复
热议问题