0

here is what I want to achieve: I want to export the view of a ViewController into a size fittest to social media, let's say a square for Instagram.

I've tried to create a new instance of my UIViewController, embedded in a freeform UIViewController, but I've trouble forcing the size.

This would simplify my work as my ViewController is instantiated in a Storyboard, and is an aggregate of multiple views, with all the population work done in the VC.

Can it work? Is that the correct approach?

lorenzo
  • 1,487
  • 1
  • 17
  • 25

3 Answers3

0

Sounds like it could work, even though it can be a bit tricky to do it manually. A possible (and probably easy) solution is to create a container view in Interface Builder that loads your view controller for you. You can then choose whatever size you want on your container, and it's content should follow.

If you don't want it visible, you could probably place it behind another view, then take a snapshot of its view property, even if it's not visible.

Note that if you do it in Interface Builder, and want get a handle to the view controller (for the purpose of setting it up, for example), you need to do it through prepareForSegue(), since the container view is not your actual view controller or even its view, it is its own view.

More on container views

Also see this answer (and comments) on how to do container views by code.

LGP
  • 4,135
  • 1
  • 22
  • 34
0

You need to set constraints between current view controller and the embedded view controller. You also have to turn off translating autoresizing mask into constraints otherwise you will get double constraints and the view will not be positioned correctly.

This is a snippet that places a smaller view inside a view in current view controller:

if let vc = storyboard?.instantiateViewController(withIdentifier: "SmallViewController") as? SmallViewController {
    // vc.view is the view of the embedded view controller
    vc.view.translatesAutoresizingMaskIntoConstraints = false
    // grayView is the small view in current view controller, that will contain the embedded view controller's view
    grayView.addSubview(vc.view)
    // define constraints between vc.view and grayView (top, bottom, left, right)
    var constraints = [NSLayoutConstraint]()
    constraints.append(NSLayoutConstraint(item: vc.view, attribute: .leading, relatedBy: .equal, toItem: grayView, attribute: .leading, multiplier: 1, constant: 0))
    constraints.append(NSLayoutConstraint(item: vc.view, attribute: .trailing, relatedBy: .equal, toItem: grayView, attribute: .trailing, multiplier: 1, constant: 0))
    constraints.append(NSLayoutConstraint(item: vc.view, attribute: .top, relatedBy: .equal, toItem: grayView, attribute: .top, multiplier: 1, constant: 0))
    constraints.append(NSLayoutConstraint(item: vc.view, attribute: .bottom, relatedBy: .equal, toItem: grayView, attribute: .bottom, multiplier: 1, constant: 0))
    grayView.addConstraints(constraints)
}

I highly recommend using PureLayout for making constraints in code as it is much easier and more readable

frin
  • 4,474
  • 3
  • 31
  • 23
  • Thanks! FYI you can define constraints as such now: view.leadingAnchor.constraint(equalTo: superView.leadingAnchor).isActive = true – lorenzo Mar 06 '18 at 13:46
0

I've actually fixed my issue by simply setting the frame after instantiating the VC from the storyboard

embedderViewController.view.frame = CGRect(x: 0, y: 0, width: 320, height: 360)

Thanks for the answers though :)

lorenzo
  • 1,487
  • 1
  • 17
  • 25