目前小 viewController
Present small viewController
问题状态:我有两个 viewController
- parentViewController (375 * 667)
- 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)
}
但它像
一样伸展自己
谁能帮我解决这个问题?
谢谢
通过设置preferredContentSize
属性 将解决您的问题,请尝试下面的代码片段
newVC?.preferredContentSize = CGSize(width: 300, height: 667)
你在哪里设置你的 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)
}
你可以这样试试
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)
问题状态:我有两个 viewController
- parentViewController (375 * 667)
- 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)
}
但它像
一样伸展自己谁能帮我解决这个问题? 谢谢
通过设置preferredContentSize
属性 将解决您的问题,请尝试下面的代码片段
newVC?.preferredContentSize = CGSize(width: 300, height: 667)
你在哪里设置你的 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)
}
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)