[英]How to place auto layout constraints inside UIPageViewController programmatically?
I am trying to get to display content in a UIPageViewController, but I had no luck so far.我试图在 UIPageViewController 中显示内容,但到目前为止我还没有运气。 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.在我的代码中,我有一个继承自 UIPageViewController 的 PageViewController 和继承自 UIViewController 的 InPageViewController 作为父视图内的内容页面。
Inside InPageViewController:在 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.这是我用来显示背景颜色的当前 state,但不是 label。 Whenever I add the constraints array, the background disappears, and reverts to its default grey colour, which I have set in PageViewController
.每当我添加约束数组时,背景就会消失,并恢复为我在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.一般来说,我对编程比较陌生,我仍然无法理解 Swift 中的自动布局逻辑,因此不胜感激。
You need to set labelView.translatesAutoresizingMaskIntoConstraints
to false
instead of view.translatesAutoresizingMaskIntoConstraints
您需要将labelView.translatesAutoresizingMaskIntoConstraints
设置为false
而不是view.translatesAutoresizingMaskIntoConstraints
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.