UIlabel layer.cornerRadius 在 iOS 7.1 中不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22316836/
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 23:41:13  来源:igfitidea点击:

UIlabel layer.cornerRadius not working in iOS 7.1

iosuiviewios7uilabelcornerradius

提问by Mike V

I'm currently looking at a UILabel with the property addMessageLabel.layer.cornerRadius = 5.0f;On a device with iOS 7.0 installed, it has rounded corners. On a device with iOS 7.1 installed, it does not have rounded corners.

我目前正在查看一个带有属性的 UILabeladdMessageLabel.layer.cornerRadius = 5.0f;在安装了 iOS 7.0 的设备上,它有圆角。在安装了 iOS 7.1 的设备上,它没有圆角。

Is this just a bug with iOS 7.1?

这只是 iOS 7.1 的一个错误吗?

回答by Raheel Sadiq

Set the property clipsToBoundsto true

将属性设置clipsToBounds为 true

addMessageLabel.clipsToBounds = true

回答by Allen

I think the best way to set corner radius is:

我认为设置拐角半径的最佳方法是:

enter image description here

在此处输入图片说明

and be sure the "Clip Subviews" is checked:

并确保选中“剪辑子视图”:

enter image description here

在此处输入图片说明

Checking "Clip Subviews" is equal to the code addMessageLabel.clipsToBounds = YES;.

检查“剪辑子视图”等于代码addMessageLabel.clipsToBounds = YES;

回答by Tapas Pal

Try the followings,

尝试以下方法,

[[addMessageLabel layer] setCornerRadius:5.0f];
[[addMessageLabel layer] setMasksToBounds:YES];

//or
[addMessageLabel setClipsToBounds:YES];

Swift

迅速

addMessageLable.layer.cornerRadius = 5.0
addMessageLable.layer.masksToBounds = true

//or
addMessageLable.layer.clipsToBounds = true

回答by Honey

My issue was a bit different.

我的问题有点不同。

While I diddo btn.clipsToBounds = true

虽然我btn.clipsToBounds = true

I wasn't setting doing:

我没有设置做:

btn.layer.cornerRadius = 20

Because I had different screen sizes. Instead I followed thisanswer and did:

因为我有不同的屏幕尺寸。相反,我遵循了这个答案并做了:

override func layoutSubviews() {
    seeMoreButton.layer.cornerRadius = seeMoreButton.bounds.size.height / 2
}

It wasn't working because I forgot to add super.layoutSubviews(). The correct code is:

它不起作用,因为我忘记添加super.layoutSubviews(). 正确的代码是:

override func layoutSubviews() {
    super.layoutSubviews()
    seeMoreButton.layer.cornerRadius = seeMoreButton.bounds.size.height / 2
}

回答by Mano Rajendran

I have tried the below one and i got an successful output.

我已经尝试了下面的一个,我得到了一个成功的输出。

yourlabelname.layer.cornerRadius = 10.0f;
[yourlabelname setClipsToBounds:YES];

Is there something else which is stopping you?

还有什么阻止你的吗?

回答by Naishta

 //works perfect in Swift 2.0 for a circular or round image          


@IBOutlet var theImage: UIImageView!
        override func viewDidLoad() {
            super.viewDidLoad()
    //Make sure the width and height are same
            self.theImage.layer.cornerRadius = self.theImage.frame.size.width / 2
            self.theImage.layer.borderWidth = 2.0
            self.theImage.layer.borderColor = UIColor.whiteColor().CGColor
            self.theImage.clipsToBounds = true

        }

回答by iAmita Singh

yourlabelname.layer.cornerRadius = yourlabelname.frame.size.width/2;
[yourlabelname setClipsToBounds:YES];

Make sure you are checking with appropriate Deployment target.

确保您正在检查适当的部署目标。

回答by BharathRao

Add the Following Code as extension for UIView

添加以下代码作为 UIView 的扩展

//// Story board Extra Feature for create border radius, border width and border Color
extension UIView {
    /// corner radius
    @IBInspectable var borderColor: UIColor? {
        set {
            layer.borderColor = newValue!.cgColor
        }
        get {
            if let color = layer.borderColor {
                return UIColor(cgColor: color)
            } else {
                return nil
            }
        }
    }
    @IBInspectable var borderWidth: CGFloat {
        set {
            layer.borderWidth = newValue
        }
        get {
            return layer.borderWidth
        }
    }
    @IBInspectable var cornerRadius: CGFloat {
        set {
            layer.cornerRadius = newValue
            clipsToBounds = newValue > 0
        }
        get {
            return layer.cornerRadius
        }
    }
}

After that you will get the following attributes in interface builder itself.!

之后,您将在界面构建器本身中获得以下属性。!

enter image description here

在此处输入图片说明