Hello
I'm in trouble because I can't find it even if I look into it.
I would like to ask you two questions about creating a view of xib.
(Execution Xcode 8.2.1/Swift 3.0.2)
class CustomView:UIView {
required init?(coder adecoder:NSCoder) {
super.init(coder:aDecoder)
let view = Bundle.main.loadNibNamed("Custom", owner:self, options:nil) ?.first as!UIView
view.frame=bounds
addSubview(view)
}
}
By loading the View of the xib file in this way, adding it as a child of the view, and resizing it,
I was looking into what CustomView is in the article.Also, here
translatesAutoresizingMaskIntoConstraints=false
and Disable AutoResizeMask AutoResizeMask Automatic Constraint Add
Some articles set up AutoLayout.
Either way, the size is adjusted and the size is the same at runtime.
view.frame=bounds
If so, the layout such as the size of the View should not be determined at the time of init.
I prepared the iPhone 5 size (320x568) ViewController on the Storyboard and
CustomView limits View Controller's view to 0 up, down, left, and right.
I ran it on iPhone 6+ (414x736).
Of course, the results of the execution show CustomView full screen, but
view.frame=bounds
Even if you do this before the layout is finalized,
(
Before viewDidLayoutSubViews, which determine the size of the managed ViewController, etc.
This init is called)
I don't know why the size fits perfectly.
print(view.frame.size)
and so on, but 320 x 568 is displayed here.
(Actual runtime size is 414x736)
Views with AutoLayout configured on IB in an article automatically
translatesAutoresizingMaskIntoConstraints=false
is configured and
view.frame = CGRect (x:100, y:100, width:100, height:100)
As shown in , there is a possibility that you will not be able to make changes directly, and I feel that I have experienced such things myself.However, I can now adjust the position and size directly with frame regardless of translatesAutoresizingMaskIntoConstraints, but has it been changed with iOS 10 or Xcode 8.0?
I reviewed the UIView reference a little bit.Below are the functions and properties you are interested in.
If you want to specify directly, such as frames,
layoutSubViews() within a custom class.
I understand that it's better to override...Summary of iOS layout
class CustomView:UIView {
var subView —UIView!
required init?(coder adecoder:NSCoder) {
super.init(coder:aDecoder)
subView= Bundle.main.loadNibNamed("Custom", owner:self, options:nil) ?.first as!UIView
// Disable automatic resizing.
autoresizesSubviews=false
// View size is 320 x 568 for IB (6+ does not match 320 x 568) when running:
// subView.frame=bounds
addSubview(subView)
}
// Even if automatic resize is disabled, AutoLayout is set to CustomView on IB, so it is called when layout is confirmed and the size fits perfectly.
// (Even if you set autoresizesSubviews=false for the View Controller, it fits perfectly.
// We found that AutoLayout determines the layout.)
override funclayoutSubviews() {
subView.frame=bounds
}
}
I've been trying to get around here in a vague way, but after seeing your question, I looked into this and that.
iOS View Programming Guide
View Programming Guide for iOS
Even the UIView
class reference is quite large, so if I write only the conclusion, the "size fits perfectly" behavior is
autoresizesSubviews
propertiesautoresizingMask
PropertiesIt is determined by the value of …
Each inspector on the storyboard/xib has its own
corresponds to
Therefore, regarding first, it is probably because the settings for each of the above properties are true
/up/down/left/right margins are fixed (both initial state in the storyboard/xib editor).
As mentioned below, even if translatesAutoresizingMaskIntoConstraints
is set to false
, Autoresizing works (without restrictions).
Regarding the second, the value of translatesAutoresizingMaskIntoConstraints
indicates the behavior of Autoresizing above
The subtle behavior may have changed depending on the iOS (and SDK) version (the constraint resolution algorithm definitely changes, and some versions may have experienced layout corruption), but if you directly manipulate frame
, you can't just say translatesAutoresizingMaskIntoConstraints
.
(I'm sorry, but I can't find a description of the order in which frame
is directly specified, traditional autoresizing, and constraints are resolved.)
So I don't think the answer to your second question is correct, but this is just a quick report.
© 2024 OneMinuteCode. All rights reserved.