I have a requirement on showing a banner view to show a message,
Now the message's content may vary and the view should also resize depending on UILabels content.
UILabel is set to Word Wrap and numberOfLines is set to 0
And the respective class file is,
import UIKit
class ORABannerView: UIView {
@IBOutlet weak var bannerText: UILabel!
static func instantiate(message: String) -> ORABannerView {
let view: ORABannerView = initFromNib() ?? ORABannerView()
view.bannerText.text = message
return view
}
override init(frame: CGRect) {
super.init(frame: frame)
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
override func draw(_ rect: CGRect) {
super.draw(rect)
}
}
The initFromNib is an implemented as UIView's extension,
extension UIView {
class func initFromNib<T: UIView>() -> T? {
guard let view = Bundle.main.loadNibNamed(String(describing: self), owner: nil, options: nil)?[0] as? T else {
return nil
}
return view
}
}
Tried with layoutIfNeeded() on the view, but it's not working for me.
Any suggestions will be appreciated.



