How to set UIView size to match parent without constraints programmatically

34,274

Solution 1

So many answers and nobody is explaining what's wrong.

I will try.

You are setting the frame of newView to your superviews frame before the autolayout engine has started to determine your superviews position and size. So, when you use the superviews frame, you are using its initial frame. Which is not correct in most cases.

You have 3 ways to do it correctly:

  • Use autolayout constraints for your newView

  • Set newViews frame in the viewDidLayoutSubviews method. Which is called when the autolayout engine finishes determining the frames actual values. (Note: This method can be called multiple times)

  • Set an autoresizing mask for newView

Solution 2

Try this:

Swift 1 and 2:

newView.autoresizingMask = [.FlexibleWidth, .FlexibleHeight]

Swift 3+:

newView.autoresizingMask = [.flexibleWidth, .flexibleHeight]

If it doesn't work, also this:

iBag.autoresizesSubviews = true

Solution 3

Try this

subview.frame = parentView.bounds

subview.autoresizingMask = [.FlexibleWidth, .FlexibleHeight]

parentView.addSubview(subview)

Solution 4

May be this will help you

override func viewDidLayoutSubviews() {

    let newView = UIView()
    newView.frame = iBag.bounds
    newView.addConstraints(iBag.constraints)
    newView.backgroundColor = UIColor.redColor()
    iBag.addSubview(newView)

}

Hope this will help you.

Solution 5

Try this,

 override func viewDidLayoutSubviews() {


    newView.frame =  (frame: CGRect(x: 0, y: 0, width: iBag.frame.width, height: iBag.frame.height))

}

or you can use viewWillAppear or viewDidAppear to set frame.

Because in viewDidload your iBag's frame is same which is in your interface builder (storyboard). Now it will change according to your constraints and device size on which you run. So, in viewDidload your new view get old frame not changed frame. So, it's better to use viewDidLayoutSubviews or viewWilAppear.

Second thing, Either use autolayout everywhere or nowhere. It is bad practice to use autolayout in parent view and not to child.

Share:
34,274

Related videos on Youtube

Seifolahi
Author by

Seifolahi

Updated on July 09, 2022

Comments

  • Seifolahi
    Seifolahi almost 2 years

    The problem sounds easy but it is making me crazy. I've created a white view in IB that's called iBag and by constraints it's size depends on screen size.

    enter image description here

    Now I want create a new UIView programmatically and add as subview to iBag with same size and position by this code

    let newView = UIView()
    newView.frame =  (frame: CGRect(x: 0, y: 0, width: iBag.frame.width, height: iBag.frame.height))
    newView.backgroundColor = UIColor.redColor()
    iBag.addSubview(newView)
    

    enter image description here

    I also tried bounds but that didn't help. I can use constraints to solve the problem but i want to understand what's wrong.

    • Sandeep Bhandari
      Sandeep Bhandari almost 8 years
      Where are you instantiating the new view in ViewWillAppear ???
    • Er.Shreyansh Shah
      Er.Shreyansh Shah almost 8 years
      This is because of constrains.
    • Paulw11
      Paulw11 almost 8 years
      You should add the new view in viewDidLayoutSubviews as the frame & autolayout process won't be complete before then. Make sure you keep a reference to your new view and only add it if this is nil as viewDidLayoutSubviews may be called more than once
  • Seifolahi
    Seifolahi almost 8 years
    Nope , same result.
  • Seifolahi
    Seifolahi almost 8 years
    Sorry but still no result
  • Nirav D
    Nirav D almost 8 years
    Are you using auto resize in storyboard or constraints layout?
  • Seifolahi
    Seifolahi almost 8 years
    After trying different things it worked but it only works in viewDidLayoutSubviews function. Anyway Thank you.
  • jn-se
    jn-se almost 7 years
    That is not a good solution because viewDidLayoutSubviews is called multiple times. So there will be many "newViews" created.
  • Ketan Parmar
    Ketan Parmar almost 7 years
    yeah that's right @jn-se Edited my answer! Now, it is fine i think
  • user3729264
    user3729264 over 4 years
    Thank you. You saved my weekend. All tutorials tells how to format UIView and no one tells that update must be called on viewDidLayoutSubviews instead of viewDidLoad
  • jspizziri
    jspizziri about 2 years
    UIView.addSubview only accepts one parameter according to the documentation: developer.apple.com/documentation/uikit/uiview/…

Related