简体   繁体   English

如何以编程方式在 UIPageViewController 中放置自动布局约束?

[英]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.

相关问题 如何以编程方式向UINavigationBar添加自动布局约束 - How to add auto layout constraints to UINavigationBar programmatically 如何快速设置UITextView for Universal App的程序化自动布局约束 - How To Set Auto Layout Constraints Programmatically for UITextView For Universal App with swift 模糊的自动布局约束(以编程方式添加) - Ambiguous Auto Layout Constraints (Programmatically added) 以编程方式使用自动布局约束的问题 - Issue using Auto Layout Constraints Programmatically 如何以编程方式创建布局约束 - How to Create layout constraints programmatically 如何以编程方式在 UIStackView 中添加响应式 UIPageViewController? - How to add a responsive UIPageViewController inside an UIStackView programmatically? 堆栈视图内的布局约束(以编程方式快速) - Layout constraints inside a stackview (swift programmatically) UIPageViewController +自动布局旋转问题 - UIPageViewController + Auto Layout rotation issue 子视图以自动编程约束以编程方式使视图中间垂直对齐 - Subview vertical align middle in view with auto layout constraints programmatically 以编程方式创建用于不同尺寸类的具有不同常量的自动布局约束 - Programmatically create auto layout constraints with different constants for different size classes
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM