iosswiftuibuttonlayoutsubviews

What is the ideal place to set edge insets of UIButton when its subview of a UITableViewCell or subview of Any other View


I have a scenario where I need to change UIEdgeInsets for UIButton which is added to the UITableViewCell, I set it in layoutSubviews but that's called a number of times, it also work's with init too but I am not sure that's really a place because that will called only once.

override func layoutSubviews() {
        super.layoutSubviews()
        if imageView != nil {
            titleEdgeInsets = UIEdgeInsets(top: 52, left: -10, bottom: 0, right:-10)
        }
    }

Can someone tell me which one is the ideal place and why?


Solution

  • awakeFromNib will be called when a new cell is loaded from a xib or storyboard , but not when a cell is re-used. But during awakeFromNib the frame layouts do not appear . UIEdgeInsets will not effected by frame so i prefer to call it at awakeFromNib not in layoutSubviews

    layoutSubviews you will get correct frame so used only with stylish that need correct frame like corner radius