MikeJ
MikeJ

Reputation: 2437

AutoLayout resize custom UIView content

I have a standard UIView added via IB which changes size based on orientation using constraints. Programatically I add a custom component (sub-classing UIView) to this view which by default is the sam size as its parent view. Problem is, how do I get my custom view (programatically added) to update its size based to match its parent when the parents size changes (due to orientation change)?

@IBOutlet weak var cardView: UIView!
override func viewDidLoad() {
    super.viewDidLoad()

    let frame = cardView.bounds
    // Custom component sub classes UI View
    // Should always be same size as its parent
    let imageTemplateView: ImageTemplateView = ImageTemplateView(frame: frame)

    cardView.addSubview(imageTemplateView)
}

Upvotes: 0

Views: 1174

Answers (4)

Gokul G
Gokul G

Reputation: 2096

Here comes viewDidLayoutSubviewsto resolve our issue,

Description:

When the bounds change for a view controller's view, the view adjusts the positions of its subviews and then the system calls this method.

Instead of viewDidLoad use viewDidLayoutSubviewsas follows

override func viewDidLayoutSubviews() {
    super.viewDidLayoutSubviews()
    let imageTemplateView: ImageTemplateView = ImageTemplateView(frame: frame)
    cardView.addSubview(imageTemplateView)
    imageTemplateView.clipsToBounds = true
}

UPDATE:

  • Declare variable above viewdidload

    var imageTemplateView: ImageTemplateView = ImageTemplateView()
    
  • set frame and add it as sub view in viewdidload

    override func viewDidLoad() {
      imageTemplateView = ImageTemplateView(frame: frame)
      cardView.addSubview(imageTemplateView)
      imageTemplateView.clipsToBounds = true
    }
    
  • In viewDidLayoutSubviews just set the frame alone,so that whenever orientation changes ImageTemplateView's frame will be updated

    override func viewDidLayoutSubviews() {
      super.viewDidLayoutSubviews()
      imageTemplateView = ImageTemplateView(frame: frame)
    }
    

Upvotes: 1

Ravi
Ravi

Reputation: 2451

try this

@IBOutlet weak var cardView: UIView!
var imageTemplateView:ImageTemplateView!

override func viewDidLoad() 
{
    super.viewDidLoad()
    imageTemplateView = ImageTemplateView(frame: cardView.bounds)
    cardView.addSubview(imageTemplateView)
}

override func viewDidLayoutSubviews() 
{
    super.viewDidLayoutSubviews()
    imageTemplateView.frame = cardView.bounds
}

Upvotes: 1

Ketan Parmar
Ketan Parmar

Reputation: 27428

Set your programmatically added view's top,bottom,leading and trailing constraints with constant 0 of each. So, it will always keep same size with respect to parent view that you have added in IB. If you want rational(like half height and width of parent view or anything else) height or width then you can manage it by playing with constant and multiplier.

If you don't have idea that how to add constraints programmatically then you can refer This SO post.

Upvotes: 1

kb920
kb920

Reputation: 3089

You can use Size Class feature. Set constraint and add view base or orientation change. Check this Tutorial http://www.hossamghareeb.com/size-classes-tutorial/

Upvotes: 0

Related Questions