Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamic corner Radius for landscape and portrait

I create UIView Extension with @IBInspectable to set corner radius but when I just rotate my device its not working properly

import UIKit
import Foundation

extension UIView {

@IBInspectable
var cornerRadius: CGFloat {
     get {
        return layer.cornerRadius
     }
     set {
        layer.cornerRadius = newValue
     }
   }
}

I took image view with following constraints enter image description here

when I run app in portrait mode, output will be enter image description here

but its not work when i rotate my device enter image description here

like image 562
Nirav Hathi Avatar asked Aug 02 '18 11:08

Nirav Hathi


2 Answers

Set the corner radius in layoutSubviews method.

If you need the view in circular shape, set corner radius as it's half width/height.

view.layer.cornerRadius = view.frame.width / 2.0
like image 184
Lal Krishna Avatar answered Sep 28 '22 00:09

Lal Krishna


I can see in your screen Shot you are providing fixed corner radius

enter image description here

also providing equal width or height to superview with multiplier so that when you rotate dimensions of UIView changed and you got this result.

you need to reset the corner radius of the view when orientation changes in self.layoutSubviews() method.

like image 23
Simran Kour Avatar answered Sep 28 '22 00:09

Simran Kour