简体   繁体   English

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

[英]UIlabel layer.cornerRadius not working in iOS 7.1

I'm currently looking at a UILabel with the property addMessageLabel.layer.cornerRadius = 5.0f;我目前正在查看具有属性addMessageLabel.layer.cornerRadius = 5.0f;的 UILabel addMessageLabel.layer.cornerRadius = 5.0f; On a device with iOS 7.0 installed, it has rounded corners.在安装了 iOS 7.0 的设备上,它有圆角。 On a device with iOS 7.1 installed, it does not have rounded corners.在安装了 iOS 7.1 的设备上,它没有圆角。

Is this just a bug with iOS 7.1?这只是 iOS 7.1 的一个错误吗?

将属性clipsToBounds设置为 true

addMessageLabel.clipsToBounds = true

I think the best way to set corner radius is:我认为设置拐角半径的最佳方法是:

在此处输入图片说明

and be sure the "Clip Subviews" is checked:并确保选中“剪辑子视图”:

在此处输入图片说明

Checking "Clip Subviews" is equal to the code addMessageLabel.clipsToBounds = YES;检查“剪辑子视图”等于代码addMessageLabel.clipsToBounds = YES; . .

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

My issue was a bit different.我的问题有点不同。

While I did do btn.clipsToBounds = true虽然我确实做了btn.clipsToBounds = true

I wasn't setting doing:我没有设置做:

btn.layer.cornerRadius = 20

Because I had different screen sizes.因为我有不同的屏幕尺寸。 Instead I followed this answer and did:相反,我遵循了这个答案并做了:

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

It wasn't working because I forgot to add super.layoutSubviews() .它不起作用,因为我忘记添加super.layoutSubviews() The correct code is:正确的代码是:

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

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?还有什么阻止你的吗?

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

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

Make sure you are checking with appropriate Deployment target.确保您正在检查适当的部署目标。

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.!之后,您将在界面构建器本身中获得以下属性。!

在此处输入图片说明

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

相关问题 layer.cornerRadius与NSLayoutConstraints(swift 3)不兼容 - layer.cornerRadius not working in conjunction with NSLayoutConstraints (swift 3) IOS 7.0:如何更改导航按钮的layer.cornerRadius的值 - IOS 7.0 : How to change values of layer.cornerRadius of a Navigation Button iOS如何删除细边框线然后使用layer.cornerRadius - iOS how to remove thin border line then use layer.cornerRadius 较小尺寸的按钮不具有layer.cornerRadius圆形 - Button at smaller size not round with layer.cornerRadius 如何在iOS 7.1.1中用layer.cornerRadius舍入的UIView中消除细边框? - How to get rid of thin border in a UIView rounded with layer.cornerRadius in iOS 7.1.1? UIView Animation 完成后移除 layer.cornerRadius - UIView Animation Removes layer.cornerRadius After Completion UIButton的layer.cornerRadius删除按钮标题的可见性 - UIButton's layer.cornerRadius removes visibility of button title UIView的子类,不能设置它自己的layer.cornerRadius? - Subclass of UIView, can't set it's own layer.cornerRadius? UILabel的Corner Radius属性在iOS 7.1中不起作用 - Corner Radius property of UILabel is not working in iOS 7.1 view.Layer.CornerRadius 不适用于 UIView Swift 3 iOS 的 UIScrollView 子视图 - view.Layer.CornerRadius not working on UIScrollView subView of UIView Swift 3 iOS
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM