我想在一个UIView中嵌入一个UIViewController,我希望能够以编程方式创建它。我已经在StoryBoard中创建了UIViewController。
创建空UIView的代码:
创建空UIView的代码:
let myNewView=UIView(frame: CGRect(x: (0 + screenHeight / 2), y: leftView.frame.origin.y, width: screenHeight / 2, height: leftView.frame.height))
myNewView.backgroundColor=UIColor.lightGray
self.view.addSubview(myNewView)
用于将UIViewController附加到视图的代码:
let storyboard = UIStoryboard(name: "Main", bundle: nil)
var controller: UIViewController = storyboard.instantiateViewController(withIdentifier: "testView") as UIViewController
myNewView.addSubview(controller.view)
这会显示我的UIView中的视图,但不是正确的方式。在此情况下,UIView的宽度为512个像素。而(embeded) UIViewcontroller则认为其宽度为1024个像素(全屏宽度)。
我该如何修复嵌入视图从其父级(UIView)获取宽度和高度的问题?