调整UIViewController的大小以导出其视图

问题描述 投票:0回答:3

这就是我想要实现的目标:我想将ViewController的视图导出到适合社交媒体的大小,让我们说一个适合Instagram的广场。

我试图创建一个我的UIViewController的新实例,嵌入在自由形式的UIViewController中,但是我很难强迫它的大小。

这将简化我的工作,因为我的ViewController在Storyboard中实例化,并且是多个视图的集合,所有人口都在VC中完成。

可以吗?这是正确的方法吗?

ios swift uiviewcontroller
3个回答
0
投票

听起来它可以工作,即使手动执行它可能有点棘手。一种可能的(也可能是简单的)解决方案是在Interface Builder中创建一个容器视图,为您加载视图控制器。然后,您可以在容器上选择任何尺寸,并且内容应该遵循。

如果您不希望它可见,您可以将它放在另一个视图后面,然后拍摄其view属性的快照,即使它不可见。

请注意,如果您在Interface Builder中执行此操作,并且想要获取视图控制器的句柄(例如,为了设置它),则需要通过prepareForSegue()执行此操作,因为容器视图不是您的实际视图控制器甚至它的观点,这是它自己的观点。

More on container views

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


0
投票

您需要在当前视图控制器和嵌入式视图控制器之间设置约束。您还必须关闭将自动调整遮罩转换为约束,否则您将获得双重约束并且视图将无法正确定位。

这是一个片段,在当前视图控制器的视图中放置一个较小的视图:

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)
}

我强烈建议使用PureLayout在代码中制作约束,因为它更容易和更易读


0
投票

我实际上通过在故事板中实例化VC后设置框架来解决我的问题

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

谢谢你的答案:)

© www.soinside.com 2019 - 2024. All rights reserved.