I have used a PageViewController
for three controllers with scroll type. Now I have called this PageViewController in a containerView
, here the issue is the container height is less than that of PageViewController, Im able to call the PageViewController in the containerView
but the bottom is getting out of the view. I tried to put autolayouts programatically but it didn't work. Let me know how to solve this?
@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])
}
the above code *******temp fix:********
solves my problem for a single device screen but not for all devices. so please let me know a proper solution for this
The Below code solved my issue.
var controller: ViewController!
override func viewDidLoad() {
super.viewDidLoad()
let storyboard = UIStoryboard.init(name: "Main", bundle: nil)
controller = storyboard.instantiateViewController(withIdentifier: "ViewController") as! ViewController
addChild(controller)
self.containerView.addSubview(controller.view)
self.setControllerFrame(controller: controller )
}
func setControllerFrame(controller: UIViewController){
self.view.setNeedsDisplay()
self.view.setNeedsLayout()
controller.view.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
controller.view.topAnchor.constraint(equalTo: self.containerView.topAnchor, constant: 0),
controller.view.bottomAnchor.constraint(equalTo: self.containerView.bottomAnchor, constant: 0),
controller.view.leadingAnchor.constraint(equalTo: self.containerView.leadingAnchor, constant: 0),
controller.view.trailingAnchor.constraint(equalTo: self.containerView.trailingAnchor, constant: 0)
])
}