How to round the corners of a button

前端 未结 15 643
隐瞒了意图╮
隐瞒了意图╮ 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:30
    UIButton* closeBtn = [[UIButton alloc] initWithFrame:CGRectMake(10, 50, 90, 35)];
    //Customise this button as you wish then
    closeBtn.layer.cornerRadius = 10;
    closeBtn.layer.masksToBounds = YES;//Important
    
    0 讨论(0)
  • 2020-12-07 07:33

    updated for Swift 3 :

    used below code to make UIButton corner round:

     yourButtonOutletName.layer.cornerRadius = 0.3 *
            yourButtonOutletName.frame.size.height
    
    0 讨论(0)
  • 2020-12-07 07:37

    You can achieve by this RunTime Attributes

    enter image description here

    we can make custom button.just see screenshot attached.

    kindly pay attention :

    in runtime attributes to change color of border follow this instruction

    1. create category class of CALayer

    2. in h file

      @property(nonatomic, assign) UIColor* borderIBColor;
      
    3. in m file:

      -(void)setBorderIBColor:(UIColor*)color {
          self.borderColor = color.CGColor;
      }
      
      -(UIColor*)borderIBColor {
          return [UIColor colorWithCGColor:self.borderColor];
      }
      

    now onwards to set border color check screenshot

    thanks

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

    Pushing to the limits corner radius up to get a circle:

        self.btnFoldButton.layer.cornerRadius = self.btnFoldButton.frame.height/2.0;
    

    If button frame is an square it does not matter frame.height or frame.width. Otherwise use the largest of both ones.

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

    Import QuartCore framework if it is not there in your existing project, then import #import <QuartzCore/QuartzCore.h> in viewcontroller.m

    UIButton *button = [[UIButton buttonWithType:UIButtonTypeRoundedRect]];
    CGRect frame = CGRectMake(x, y, width, height); // set values as per your requirement
    button.layer.cornerRadius = 10;
    button.clipsToBounds = YES;
    
    0 讨论(0)
  • 2020-12-07 07:41

    You may want to check out my library called DCKit. It's written on the latest version of Swift.

    You'd be able to make a rounded corner button/text field from the Interface builder directly:

    It also has many other cool features, such as text fields with validation, controls with borders, dashed borders, circle and hairline views etc.

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