-2

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 enter image description here

AtulParmar
  • 4,358
  • 1
  • 24
  • 45
s60
  • 37
  • 2
  • 7

6 Answers6

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
  • try this v1.frame = view.bounds (here view is your superview) – Bhavi Lad Dec 12 '17 at 10:36
  • 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]))