【问题标题】:Present small viewController呈现小viewController
【发布时间】:2017-03-21 07:33:14
【问题描述】:

问题状态:我有两个 viewController

  1. parentViewController (375 * 667)
  2. childViewController (300 * 667)

我想在 Axis (75 * 0) 处显示 childViewController,如下所示

childViewController 显示在这个 IBAction 上

@IBAction func btnShowVC(_ sender: UIButton) {
    let storyboard = UIStoryboard(name: "Main", bundle: nil)
    let newVC = storyboard.instantiateViewController(withIdentifier: "cvc") as? childViewController
    self.present(newVC!, animated: true, completion: nil)
}

但它会像自己一样伸展

谁能帮我解决这个问题? 谢谢

【问题讨论】:

  • 我认为使用简单的子视图会更好,如果你想使用 viewcontroller 则可以尝试搜索 popover 演示

标签: ios swift ipad viewcontroller cgpoint


【解决方案1】:

你可以这样试试。

let storyboard = UIStoryboard(name: "Main", bundle: nil)
        let newVC = storyboard.instantiateViewController(withIdentifier: "ChildViewController") as? ChildViewController
            view.addSubview((newVC?.view)!)
        newVC?.view.frame = CGRect(x:75,y:0,width:view.frame.size.width-75,height:view.frame.size.height)
        newVC?.view.autoresizingMask = \[.flexibleWidth, .flexibleHeight\]


        newVC?.didMove(toParentViewController: self)

【讨论】:

  • 如果你想添加子控制器然后点击子按钮。
  • 这个想法是正确的,但代码不是。您缺少 addChildViewController 调用。
【解决方案2】:

您在哪里设置 ViewController 的框架?如果您使用自动布局,则需要设置约束以使 ViewController 看起来像您想要的那样。如果您想以编程方式执行此操作,您可以在某处设置框架,例如在您上面发布的代码中,您可以这样做:

@IBAction func btnShowVC(_ sender: UIButton) {
   let storyboard = UIStoryboard(name: "Main", bundle: nil)
   let newVC = storyboard.instantiateViewController(withIdentifier: "cvc") as? childViewController
   newVc.view.frame = CGRect(x: 75, y: 0, width: 300, height: 667)
   self.present(newVC!, animated: true, completion: nil)
}

【讨论】:

  • 它给出了这个错误:'childViewController?'类型的值没有成员“框架”
  • 对不起,我编辑了我的答案,我忘了在通话中添加视图。
【解决方案3】:

通过设置preferredContentSize属性可以解决你的问题,请试试下面的代码sn-p

newVC?.preferredContentSize = CGSize(width: 300, height: 667)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-03-07
    • 2018-02-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-13
    • 2014-12-16
    相关资源
    最近更新 更多