I am trying to add a subview into fullscreen parent view, but it does not show up in full parent view. If I don't init or don't set it inside frame, it does not show up
Asked
Active
Viewed 826 times
-2

AtulParmar
- 4,358
- 1
- 24
- 45

s60
- 37
- 2
- 7
6 Answers
1
Try this You will find something helpful Just simply add this UIView extension
extension UIView {
func fillInSuperView(inset:UIEdgeInsets = UIEdgeInsets.zero) {
self.translatesAutoresizingMaskIntoConstraints = false
self.leadingAnchor.constraint(equalTo: (self.superview?.leadingAnchor)!, constant: inset.left).isActive = true;
self.trailingAnchor.constraint(equalTo: (self.superview?.trailingAnchor)!, constant: -inset.right).isActive = true
self.topAnchor.constraint(equalTo: (self.superview?.topAnchor)!, constant: inset.top).isActive = true
self.bottomAnchor.constraint(equalTo: (self.superview?.bottomAnchor)!, constant: -inset.bottom).isActive = true
}
}
Calling of this function
YourView.fillInSuperView(inset: UIEdgeInsets.zero)
You can set padding from the the superView by setting insets

Ruchin Somal
- 1,073
- 12
- 14
0
change v1 width and height to parent width and height like
let v1 = UIView(frame: CGRect(x:0, y:0, width: view.bounds.size.width, height: view.bounds.size.height))
this may work

Faysal Ahmed
- 7,501
- 5
- 28
- 50

binay
- 1
-
i thought autolayout should auto update width height with constraints, if i need to set width, height, then what is meaning of autolayout here ? – s60 Dec 11 '17 at 07:33
0
Change v1 frame layout to full-screen layout. The height and width would be full-screen height and width. You can update frame size by using this
let v1 = UIView(frame: CGRect(x:0, y:0, width: UIScreen.main.bounds.size.width, height: UIScreen.main.bounds.size.height))
By using this no need to use Autolayout
constraints.

Faysal Ahmed
- 7,501
- 5
- 28
- 50
-
you are not understand my question, sorry. my question is why subview not show fullscreen parent view with constraint – s60 Dec 12 '17 at 10:25
0
Your mistake is that you have specified fix width and height for v1
i.e. let v1 = UIView(frame: CGRect(x:0, y:0, width: 100, height: 50))
instead try this
let v1 = UIView()
// this will take frame(x, y, width, height) from superview
v1.frame = view.frame

Bhavi Lad
- 237
- 2
- 7
-
no this, no relate to the view, whatever size you set, autolayout still apply the constraint – s60 Dec 12 '17 at 10:24
-
hmm, but this new v1 view will grab the same frame like its superview, and will fit in. – Bhavi Lad Dec 12 '17 at 10:30
-
-
what is want is when super view change, subview also autolayout with the constraints, what frame you set for the v1 doesn't important – s60 Dec 12 '17 at 10:37
0
Change v1 frame size as full view by using this
let v1 = UIView(frame: UIScreen.main.bounds)
This will work.
-
i dont want, just want v1 full screen superview when superview resize, not fullscreen – s60 Dec 12 '17 at 10:23
0
Try this,
let v1 = UIView(frame: CGRect(x: 0, y: 0, width: 100, height: 50))
v1.backgroundColor = UIColor.gray
v1.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(v1)
view.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|[v1]|", options: [], metrics: nil, views: ["v1" : v1]))
view.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:|[v1]|", options: [], metrics: nil, views: ["v1" : v1]))

Vivek Lathiya
- 76
- 7
-
-
Are you working with playground ? I added this code for iOS you can see effect of this code in UI of simulator and device. – Vivek Lathiya Dec 13 '17 at 04:32
-
i just tell you the constraint you added and the new anchor constraint did same effect, yes, i tested on playground, – s60 Dec 13 '17 at 10:10
-
Then something other issue you have. Because i have tested it in iOS simulator and it is working fine. – Vivek Lathiya Dec 13 '17 at 10:15