0

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()
    }
    
 
}
biggreentree
  • 1,633
  • 3
  • 20
  • 35
  • Did you meant `self.frame.height / 2.0` instead? What do you want to to be? Also, after subviewDidlayout, the `self.frame` might change, so the you might want to use then another CornerRadius. – Larme Mar 04 '21 at 16:58
  • with height, both sides become round, I just need classic rounding on corners. – biggreentree Mar 04 '21 at 17:01
  • "Classic rounding"? What's classic? You are trying to have a computed value on `frame`, but define classic. What's the target output? – Larme Mar 04 '21 at 17:02
  • first image is the result of hard coded 12 value, I want that effect, but computed. just like I do on normal outlets. Usually I apply self.frame.width / 2.0 but here is failing – biggreentree Mar 04 '21 at 17:05
  • That's the effect if you use `self.frame.width/2.0`, meaning if self.frame is 200, try to hard code it to 100, you'll see, that's the effect. – Larme Mar 04 '21 at 17:06
  • not sure to understand, on iPhone 12 Pro max simulator width of frame at runtime is 374.0, – biggreentree Mar 04 '21 at 17:08
  • So replace `self.contentView.layer.cornerRadius = 12.0` with `self.contentView.layer.cornerRadius = 374/2.0`, that's what you want, in reality, just it's hard coded? Well, the result UI should be the same. You don't seem to understand what means cornerRadius – Larme Mar 04 '21 at 17:09
  • I understand that the corneradius will be 2% of width. Your solution won't fit on different devices, I think. – biggreentree Mar 04 '21 at 17:12
  • Wait. 2% or divided by 2? That's different. I still don't think that you understand what means corner radius. Because you shouldn't really care about the device size, it should be a fixed size (except it you want really a rounded view/side). Could you show expected result depending on different devices? – Larme Mar 04 '21 at 17:13
  • expected result is like my first image, a rectangular view, rounded corners, view has only one height constraint, width is limited 10 on leading and trailing edges. center y axis. – biggreentree Mar 04 '21 at 21:29

0 Answers0