简体   繁体   English

如何使按钮的角变圆

[英]How to round the corners of a button

I have a rectangle image (jpg) and want to use it to fill the background of a button with rounded corner in xcode.我有一个矩形图像 (jpg) 并想用它来填充 xcode 中带有圆角的按钮的背景。

I wrote the following:我写了以下内容:

UIButton *button = [[UIButton buttonWithType:UIButtonTypeRoundedRect] retain];
CGRect frame = CGRectMake(x, y, cardWidth, cardHeight);
button.frame = frame;
[button setBackgroundImage:backImage forState:UIControlStateNormal];

However, the button I get with that approach doesn't have its corners rounded: it is instead a plain rectangle that looks exactly like my original image.然而,我用这种方法得到的按钮没有圆角:它是一个看起来与我的原始图像完全一样的普通矩形。 How can I get instead an image with rounded corner to represent my button?我怎样才能得到一个带有圆角的图像来代表我的按钮?

Thanks!谢谢!

I tried the following solution with the UITextArea and I expect this will work with UIButton as well.我在 UITextArea 上尝试了以下解决方案,我希望这也适用于 UIButton。

First of all import this in your .m file -首先在您的 .m 文件中导入它 -

#import <QuartzCore/QuartzCore.h>

and then in your loadView method add following lines然后在您的loadView方法中添加以下几行

    yourButton.layer.cornerRadius = 10; // this value vary as per your desire
    yourButton.clipsToBounds = YES;

You can achieve by this RunTime Attributes你可以通过这个 RunTime Attributes 来实现

在此处输入图片说明

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创建 CALayer 的类别类

  2. in h file在 h 文件中

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

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

now onwards to set border color check screenshot现在开始设置边框颜色检查屏幕截图

更新的答案

thanks谢谢

You may want to check out my library called DCKit .您可能想查看我的名为DCKit 的库。 It's written on the latest version of Swift .它是在最新版本的Swift上编写的。

You'd be able to make a rounded corner button/text field from the Interface builder directly:您可以直接从界面构建器制作圆角按钮/文本字段:

DCKit:圆形按钮

It also has many other cool features, such as text fields with validation, controls with borders, dashed borders, circle and hairline views etc.它还具有许多其他很酷的功能,例如带验证的文本字段、带边框的控件、虚线边框、圆形和细线视图等。

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.如果按钮框架是正方形,则frame.height 或frame.width 无关紧要。 Otherwise use the largest of both ones.否则使用两者中最大的一个。

Import QuartCore framework if it is not there in your existing project, then import #import <QuartzCore/QuartzCore.h> in viewcontroller.m如果现有项目中没有QuartCore 框架,请导入它,然后在viewcontroller.m 中导入#import <QuartzCore/QuartzCore.h>

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;
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

First set width=100 and Height=100 of button首先设置按钮的 width=100 和 Height=100

Objective C Solution目标 C 解决方案

YourBtn1.layer.cornerRadius=YourBtn1.Frame.size.width/2;
YourBtn1.layer.borderColor=[uicolor blackColor].CGColor;
YourBtn1.layer.borderWidth=1.0f;

Swift 4 Solution Swift 4 解决方案

YourBtn1.layer.cornerRadius = YourBtn1.Frame.size.width/2
YourBtn1.layer.borderColor = UIColor.black.cgColor
YourBtn1.layer.borderWidth = 1.0

Try my code.试试我的代码。 Here you can set all properties of UIButton like text colour, background colour, corner radius, etc.在这里您可以设置 UIButton 的所有属性,如文本颜色、背景颜色、圆角半径等。

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

Now call like this现在这样调用

yourBtnName.btnCorner()

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 [ObjC] – 带有圆角的 UIButton - http://goo.gl/kfzvKP

It's a XIB/Storyboard subclass.它是一个 XIB/Storyboard 子类。 Import and set borders without write code.无需编写代码即可导入和设置边框。

对于斯威夫特:

button.layer.cornerRadius = 10.0

updated for Swift 3 :为 Swift 3 更新:

used below code to make UIButton corner round:使用下面的代码使 UIButton 角变圆:

 yourButtonOutletName.layer.cornerRadius = 0.3 *
        yourButtonOutletName.frame.size.height

在此处输入图片说明

For Objective C :对于目标 C

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

For Swift :对于斯威夫特

submitButton.layer.cornerRadius = 5
submitButton.clipsToBounds = true

Swift 4 Update斯威夫特 4 更新

I also tried many options still i wasn't able to get my UIButton round cornered.我也尝试了很多选项,但仍然无法让我的 UIButton 转角。 I added the corner radius code inside the viewDidLayoutSubviews() Solved My issue.我在viewDidLayoutSubviews()添加了圆角半径代码解决了我的问题。

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

Also we can adjust the cornerRadius as follows我们也可以如下调整cornerRadius

func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()
        anyButton.layer.cornerRadius = 10 //Any suitable number as you prefer can be applied 
    }

另一个设置边框(使其更像按钮)的替代答案在这里... 如何为自定义类型 UIButton 设置矩形边框

For iOS SWift 4对于 iOS 快速 4

button.layer.cornerRadius = 25;
button.layer.masksToBounds = true;

You can use outlet connection and didSet function for your button on the view;您可以为视图上的按钮使用插座连接和 didSet 函数;

 @IBOutlet weak var button: UIButton!{
        didSet {
            button.layer.cornerRadius = 5;
            button.layer.masksToBounds = true;
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM