Custom View - self.frame is not correct?
Solution 1
Short and simple answer:
You're doing it too early.
Detailed answer:
When a view is initialized from an Interface Builder file (a xib or a storyboard) its frame is initially set to the frame it has in Interface Builder. You can look at it as a temporary placeholder.
When using Auto Layout the constraints are resolved (= the view's actual frame is computed) inside the view's layoutSubviews()
method.
Thus, there are two possible solutions for your problem:
(preferrable) If you use Auto Layout, use it throughout your view.
- Either add your
testView
in Interface Builder as well and create an outlet for it - or create your
testView
in code as you do, then set itstranslatesAutoResizingMaskIntoConstraints
property tofalse
(to sort of "activate Auto Layout") and add the required constraints for it in code.
- Either add your
Set your
testView
's frame after theMessageBox
view's frame itself has been set by the layout engine. The only place where you can be sure that the system has resolved the view's frame from the constraints is whenlayoutSubviews()
is called.override func layoutSubviews() { super.layoutSubviews() testView.frame = self.frame }
(You need to declare your
testView
as a property / global variable, of course.)
Solution 2
Try to use the anchors for your view:
MessageBox.centerXAnchor.constraintEqualToAnchor(self.view.centerXAnchor).active
= true
MessageBox.centerYAnchor.constraintEqualToAnchor(self.view.centerYAnchor).active
= true
MessageBox.widthAnchor.constraintEqualToConstant(150).active = true
MessageBox.heightAnchor.constraintEqualToConstant(100).active = true
This method have to be used inside your class
Solution 3
override func layoutSubviews() {
super.layoutSubviews()
testView.frame = self.frame
}
this also works when you add a custom class to a UIView in the storyboard and that uses autolayout. thanks Mischa !
Quantm
Updated on June 14, 2022Comments
-
Quantm almost 2 years
So I have a custom UIView class
class MessageBox: UIView { override init(frame: CGRect) { super.init(frame: frame) createSubViews() } required init?(coder aDecoder: NSCoder) { super.init(coder: aDecoder) createSubViews() } func createSubViews() { let testView = UIView(frame: self.frame) testView.backgroundColor = UIColor.brown self.addSubview(testView) } }
I added a UIView inside the storyboard and gave it some constraints:
100 from the top (superview), 0 from the left and right, height is 180
But when I run the app the brown subview I created in the code is way to big. I printed
self.frame
in my custom view and it turns out that the frame is(0,0,1000,1000)
. But why? I set constraints, it should be something like(0,0,deviceWith, 180)
.What did I do wrong?
EDIT: That's my Storyboard setup:
-
Quantm over 7 yearstried it, but the problem is that the superview's height and width is 1000. But I don't know why
-
Dasem over 7 yearsprobabley because , the layout engine has not finished it’s job yet, so you can’t rely on the frame and bounds size in the method
-
Quantm over 7 yearsThe problem is that I can't, for instance, transform the testView as
layoutSubviews()
would reset it immediately after that. I think the problem is that inside theinit?(coder aDecoder: NSCoder)
method the frame hasen't been calculated yet, thus I can't use it. I somehow have to bypass that, I think -
Quantm over 7 yearsI just posted a similar question regarding that as this could be a bit off-topic here. I will post a complete answer here if I have one. Or maybe someone else has an idea :)
-
Abhishek over 4 yearsThanks for your answer but if we need to pass some data to Custom UIView which needs to used inside layoutSubviews, how can we do this ?