簡體   English   中英

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

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

我目前正在查看具有屬性addMessageLabel.layer.cornerRadius = 5.0f;的 UILabel addMessageLabel.layer.cornerRadius = 5.0f; 在安裝了 iOS 7.0 的設備上,它有圓角。 在安裝了 iOS 7.1 的設備上,它沒有圓角。

這只是 iOS 7.1 的一個錯誤嗎?

將屬性clipsToBounds設置為 true

addMessageLabel.clipsToBounds = true

我認為設置拐角半徑的最佳方法是:

在此處輸入圖片說明

並確保選中“剪輯子視圖”:

在此處輸入圖片說明

檢查“剪輯子視圖”等於代碼addMessageLabel.clipsToBounds = YES; .

嘗試以下方法,

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

//or
[addMessageLabel setClipsToBounds:YES];

迅速

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

//or
addMessageLable.layer.clipsToBounds = true

我的問題有點不同。

雖然我確實做了btn.clipsToBounds = true

我沒有設置做:

btn.layer.cornerRadius = 20

因為我有不同的屏幕尺寸。 相反,我遵循了這個答案並做了:

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

它不起作用,因為我忘記添加super.layoutSubviews() 正確的代碼是:

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

我已經嘗試了下面的一個,我得到了一個成功的輸出。

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

還有什么阻止你的嗎?

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

確保您正在檢查適當的部署目標。

添加以下代碼作為 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
        }
    }
}

之后,您將在界面構建器本身中獲得以下屬性。!

在此處輸入圖片說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM