我已经使用了一个PageViewController
为三个控制器与滚动类型。现在我已经在containerView
中调用了这个PageViewController,这里的问题是容器的高度小于PageViewController的高度,我可以在containerView
中调用PageViewController,但底部正在脱离视图。我试图把自动布局编程,但它没有工作。让我知道如何解决这个问题?
@IBOutlet weak var containerView: UIView!
override func viewDidLoad() {
super.viewDidLoad()
let storyboard = UIStoryboard.init(name: "Main", bundle: nil)
let controller = storyboard.instantiateViewController(withIdentifier: "ViewController")
self.setControllerFrame(controller: controller )
addChild(controller)
self.containerView.addSubview(controller.view)
}
func setControllerFrame(controller: UIViewController){
//Validating frames according to orientation
self.view.setNeedsDisplay()
self.view.layoutIfNeeded()
//*******temp fix:******** this is solutions for a single device not for all devices(screen sizes).
if (self.view.frame.size.width < self.view.frame.size.height){
controller.view.frame = CGRect.init(x: 0, y: 0, width: self.view.frame.size.width, height: self.view.frame.size.height)
} else if (self.view.frame.size.width >= self.view.frame.size.height){
controller.view.frame = CGRect.init(x: 0, y: 0, width: self.view.frame.size.height, height: self.view.frame.size.width)
}
//tried this but crashes at last line.
controller.view.translatesAutoresizingMaskIntoConstraints = false
let topConstraint = NSLayoutConstraint(item: controller.view!, attribute: NSLayoutConstraint.Attribute.top, relatedBy: NSLayoutConstraint.Relation.equal, toItem: self.containerView, attribute: NSLayoutConstraint.Attribute.top, multiplier: 1, constant: 0)
let bottomConstraint = NSLayoutConstraint(item: controller.view!, attribute: NSLayoutConstraint.Attribute.bottom, relatedBy: NSLayoutConstraint.Relation.equal, toItem: self.containerView, attribute: NSLayoutConstraint.Attribute.bottom, multiplier: 1, constant: 0)
let leadingConstraint = NSLayoutConstraint(item: controller.view!, attribute: NSLayoutConstraint.Attribute.leading, relatedBy: NSLayoutConstraint.Relation.equal, toItem: self.containerView, attribute: NSLayoutConstraint.Attribute.leading, multiplier: 1, constant: 0)
let trailingConstraint = NSLayoutConstraint(item: controller.view!, attribute: NSLayoutConstraint.Attribute.trailing, relatedBy: NSLayoutConstraint.Relation.equal, toItem: self.containerView, attribute: NSLayoutConstraint.Attribute.trailing, multiplier: 1, constant: 0)
view.addConstraints([topConstraint, bottomConstraint, leadingConstraint, trailingConstraint])
}
上面的代码*******temp fix:********
解决了我在单个设备屏幕上遇到的问题,但不能解决所有设备的问题。所以请让我知道一个适当的解决办法
2条答案
按热度按时间des4xlb01#
1-在
viewDidLoad
内部frame
还不正确,因此将其设置在viewDidLayoutSubviews
内部2-这个坠毁了
因为您应该在设置约束之前添加子视图
k4ymrczo2#
下面的代码解决了我的问题。