How to access height used in wR x hR mode which I have set in Storyboard

190 Views Asked by At

I have TextField in my UI and I have given dynamic height(using Storyboard) for wC x hR and wR x hR, now when I add corner radius to the textfield programmatically textField.layer.cornerRadius = textField.frame.height/2,

I am getting the height which I have set for wC x hR. Is there any method by which I can get the actual height used in the iPad or iPhone device? I am getting 40 instead of 70 in iPad with wR x hR Use this image for reference

1

There are 1 best solutions below

0
DonMag On

You are likely setting the .cornerRadius at the wrong point in the view layout... that is, you are calculating textField.frame.height/2 before auto-layout sets the actual height.

Much easier approach is to sub-class UITextField and override layoutSubviews(), where you will update the .cornerRadius value:

class RoundTextField: UITextField {

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

}

Assign the Custom Class of your text field to RoundTextField ... in every other way, you can treat it as a standard text field.