How to set cornerRadius for only top-left and top-right corner of a UIView?

后端 未结 26 2968
佛祖请我去吃肉
佛祖请我去吃肉 2020-11-22 06:14

Is there a way to set cornerRadius for only top-left and top-right corner of a UIView?

I tried the following, but it end up not seeing the

26条回答
  •  [愿得一人]
    2020-11-22 07:14

    My solution for rounding specific corners of UIView and UITextFiels in swift is to use

    .layer.cornerRadius

    and

    layer.maskedCorners

    of actual UIView or UITextFields.

    Example:

    fileprivate func inputTextFieldStyle() {
            inputTextField.layer.masksToBounds = true
            inputTextField.layer.borderWidth = 1
            inputTextField.layer.cornerRadius = 25
            inputTextField.layer.maskedCorners = [.layerMaxXMaxYCorner,.layerMaxXMinYCorner]
            inputTextField.layer.borderColor = UIColor.white.cgColor
        }
    

    And by using

    .layerMaxXMaxYCorner

    and

    .layerMaxXMinYCorner

    , I can specify top right and bottom right corner of the UITextField to be rounded.

    You can see the result here:

提交回复
热议问题