how to correctly calculate corneRadius of a XIB in swift

60 Views Asked by At

cannot set calculated value for the roundedCorners of this xib. only works if Hard coded. So do you have solution? and more generally speaking, is this a good way to make a xib?

this works, but is hard coded

self.contentView.layer.cornerRadius = 12.0

this is too strong effect, clearly not calculating right the value.

self.contentView.layer.cornerRadius = self.frame.width / 2.0 

enter image description here

enter image description here

tested some IBInspectable, bu issue Is the same.

//    @IBInspectable
//    var autoCalculateCornerRadius: Bool = false
    
//    fileprivate var backingCornerRadius: CGFloat = 0
//    @IBInspectable
//    var cornerRadius: CGFloat {
//        set { layer.cornerRadius = newValue }
//        get { return layer.cornerRadius     }
//    }
    
//    @IBInspectable
//    var cornerRadius: CGFloat {
//        set {
//            backingCornerRadius = newValue
//            self.layer.cornerRadius = newValue
//        }
//        get {
//            return self.layer.cornerRadius
//        }
//    }
    
//    override func layoutSubviews() {
//        super.layoutSubviews()
//        if autoCalculateCornerRadius {
//            backingCornerRadius = cornerRadius
//            cornerRadius = self.frame.width / 2.0
//        } else {
//            cornerRadius = backingCornerRadius
//        }
//    }

import UIKit

class MyCustomViewXib: UIView {

    @IBOutlet weak var myImage: UIImageView!
    @IBOutlet weak var myLabel: UILabel!
    
    @IBOutlet var contentView: UIView! //the owner view itself
    
    private var viewHeight: CGFloat = 0

    
    required public init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        self.setup()
    }
    
    required override init(frame: CGRect) {
        super.init(frame: frame)
        self.setup()
    }
    
    deinit {
    }
    
    
    private func setup() {
        Bundle.main.loadNibNamed("\(Self.self)", owner: self, options: nil)
        addSubview(contentView)
//        self.backgroundColor = .clear
        viewHeight = contentView.frame.size.height
        contentView.frame = self.bounds
        contentView.translatesAutoresizingMaskIntoConstraints = false
        contentView.topAnchor.constraint(equalTo: self.topAnchor).isActive = true
        contentView.bottomAnchor.constraint(equalTo: self.bottomAnchor).isActive = true
        contentView.leadingAnchor.constraint(equalTo: self.leadingAnchor).isActive = true
        contentView.trailingAnchor.constraint(equalTo: self.trailingAnchor).isActive = true

        
        self.myLabel.backgroundColor = .red
        self.myImage.backgroundColor = .systemTeal
        self.contentView.backgroundColor = .systemGreen
        self.contentView.layer.cornerRadius = 12.0
//        self.contentView.layer.cornerRadius = self.frame.width / 2.0 too much

        self.setNeedsLayout()
    }
    
 
}
0

There are 0 best solutions below