ios 如何使按钮的角变圆

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5047818/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 18:57:05  来源:igfitidea点击:

How to round the corners of a button

ioscocoa-touchuibuttonrounded-corners

提问by double07

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!

谢谢!

回答by devsri

I think I got your problem, I tried the following solution with the UITextArea and I suppose 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 loadViewmethod add following lines

然后在您的loadView方法中添加以下行

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

Hope this works for you and if yes do communicate.

希望这对您有用,如果是,请进行交流。

回答by Krutarth Patel

You can achieve by this RunTime Attributes

你可以通过这个 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];
    }
    
  1. 创建 CALayer 的类别类

  2. 在 h 文件中

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

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

now onwards to set border color check screenshot

现在开始设置边框颜色检查屏幕截图

updated answer

更新的答案

thanks

谢谢

回答by Andrey Gordeev

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

您可能想查看我的名为DCKit 的库。它是在最新版本的Swift上编写的。

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

您可以直接从界面构建器制作圆角按钮/文本字段:

DCKit: rounded button

DCKit:圆形按钮

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

它还具有许多其他很酷的功能,例如带验证的文本字段、带边框的控件、虚线边框、圆形和细线视图等。

回答by Javier Calatrava Llavería

Pushing to the limitscorner radius up to get a circle:

推到极限角半径得到一个圆:

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

enter image description here

在此处输入图片说明

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

如果按钮框架是正方形,则frame.height 或frame.width 无关紧要。否则使用两者中最大的一个。

回答by iAnkit

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

进口QuartCore框架,如果它不存在于现有的项目,然后导入#import <QuartzCore/QuartzCore.h>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;

回答by Vinay Mahipal

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

回答by iOS Lifee

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

回答by pableiros

For Swift:

对于斯威夫特:

button.layer.cornerRadius = 10.0

回答by Kiran jadhav

updated for Swift 3 :

为 Swift 3 更新:

used below code to make UIButton corner round:

使用下面的代码使 UIButton 角变圆:

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

回答by iOS

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()