I am trying to get to display content in a UIPageViewController, but I had no luck so far. I was able to display the page view dots, and the background colour (teal) applies correctly, but that's about it.
In my code, I have a PageViewController inheriting from UIPageViewController, and InPageViewController inheriting from UIViewController as the content pages inside the parent view.
Inside InPageViewController:
private let labelView: UILabel = {
let label = UILabel()
// label.translatesAutoresizingMaskIntoConstraints = false
label.text = "Label"
return label
}()
init(data: PageData) {
super.init(nibName: nil, bundle: nil)
labelView.text = data.dataName
view.backgroundColor = .systemTeal
view.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(labelView)
// These are the constraints I am trying to set. Tried lots of different variations already
// NSLayoutConstraint.activate([
// labelView.centerXAnchor.constraint(equalTo: view.layoutMarginsGuide.centerXAnchor),
// labelView.centerYAnchor.constraint(equalTo: view.layoutMarginsGuide.centerYAnchor),
// ])
}
This is the current state that I get to display the background colour, but not the label. Whenever I add the constraints array, the background disappears, and reverts to its default grey colour, which I have set in PageViewController
.
let dataArray = [PageData(dataName: "Data"), PageData(dataName: "Lore")]
override func viewDidLoad() {
super.viewDidLoad()
setupView()
}
func setupView() {
title = "A fistful of Datas"
}
lazy var arrayInPageViewController: [InPageViewController] = {
var inPageVC = [InPageViewController]()
for data in dataArray {
inPageVC.append(InPageViewController(data: data))
}
return inPageVC
}()
override init(transitionStyle style: UIPageViewController.TransitionStyle, navigationOrientation: UIPageViewController.NavigationOrientation, options: [UIPageViewController.OptionsKey : Any]? = nil) {
super.init(transitionStyle: .scroll, navigationOrientation: navigationOrientation, options: nil)
self.dataSource = self
self.delegate = self
self.view.backgroundColor = .systemGray
setViewControllers([arrayInPageViewController[0]], direction: .forward, animated: true, completion: nil)
print(dataArray)
}
Here I am setting the parent view. I also have an extension, in which I run the stubs.
I am relatively new to programming in general, and I am still having trouble understanding the auto-layout logic in Swift, so any help is appreciated.
You need to set labelView.translatesAutoresizingMaskIntoConstraints
to false
instead of view.translatesAutoresizingMaskIntoConstraints