Reputation: 5340
I am loading a UIView from a .xib file like this:
static func loadFromNib() -> CardView {
let nib = UINib(nibName: "CardView", bundle: nil)
return nib.instantiate(withOwner: self, options: nil).first as! CardView
}
When loaded the view has the exact frame size as set in "Frame Rectangle" of the Size Inspector in the Interface Builder.
Is this guaranteed? I need this size to be exact, because subview constraints are specific and will not fit if the view has the wrong size [*], but I didn't find any mention of this in Apple's docs.
[*] = Reason: I am rendering the view to an UIImage so I can display it in and UIImageView later on. It shows the image of a membership card and name and membership number need to be in the right place with the correct font size on all devices..
Upvotes: 4
Views: 6826
Reputation: 3163
Set any UIView subclass as owner of xib, then load xib as subview of this view and set autoresizing mask.
This is how I use it:
extension UIView {
func loadXibView(with xibFrame: CGRect) -> UIView {
let className = String(describing: type(of: self))
let bundle = Bundle(for: type(of: self))
let nib = UINib(nibName: className, bundle: bundle)
guard let xibView = nib.instantiate(withOwner: self, options: nil)[0] as? UIView else {
return UIView()
}
xibView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
xibView.frame = xibFrame
return xibView
}
}
xibView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
sets size of view properly.
Then just use it any UIView subclass in initialization:
override init(frame: CGRect) {
super.init(frame: frame)
addSubview(loadXibView(with: bounds))
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
addSubview(loadXibView(with: bounds))
}
Upvotes: 4
Reputation: 888
Create a custom class for your UIView:
class CardView: UIView {
override init(frame: CGrect) {
super.init(frame: frame)
let xibView = UINib(nibName: "CardView", bundle: nil).instantiate(withOwner: nil, options:nil)[0] as! UIView
self.addSubview(xibView)
}
require init?(coder: aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
}
and then call if from the class you'll be implementing it in with the frame size you need or otherwise it will default to the size that is set in your interface builder:
// MyViewController
var cardView: CardView?
override func viewDidLoad() {
super.viewDidLoad()
self.cardView = CardView()
self.cardView.frame.size = CGSize(size here)
self.cardView.frame.origin = CGPoint(point here)
self.view.addSubview(self.cardView!)
}
Upvotes: 1