ios - 尝试以编程方式将 UIScrollView 添加到我的应用程序中,但我不断收到空白 View

标签 ios swift xcode

尝试创建一个 UIScrollView,但我似乎无法让我的标 checkout 现,因为我希望它们位于正中心。

lazy var label : UILabel = {
        let label = UILabel()
        label.text = "center of container view.center of container view"
        label.font = UIFont(name: "Bebas Neue", size: 23)!
        label.textColor = .black
        return label
    }()
  // Mark: Properties

    lazy var contentViewSize = CGSize(width: self.view.frame.width + 1200, height: self.view.frame.height)

    // Mark: Views
    fileprivate lazy var parentScrollView : UIView = {
        var newView = UIView()

        newView.translatesAutoresizingMaskIntoConstraints = false
        newView.backgroundColor = .black
        newView.frame = self.scrollView.bounds
        newView.frame.size = contentViewSize


        return newView
    }()
    fileprivate lazy var scrollView : UIScrollView = {
        var uiScrollView = UIScrollView(frame: .zero)
        uiScrollView.translatesAutoresizingMaskIntoConstraints = false
        uiScrollView.frame = view.bounds
        uiScrollView.backgroundColor = .gray
        uiScrollView.addViewBorder(borderColor: UIColor.gray.cgColor, borderWith: 10, borderCornerRadius: 0)
//        uiScrollView.alpha =
        uiScrollView.contentSize = contentViewSize
//        uiScrollView.autoresizingMask = .flexibleHeight
        uiScrollView.showsVerticalScrollIndicator = true
        uiScrollView.bounces = true

        return uiScrollView
    }()
    fileprivate lazy var tutorialView : UIStackView = {

        var tutView = UIStackView(arrangedSubviews: [label,label,label])
        tutView.axis = .horizontal
        tutView.backgroundColor = .white
        tutView.distribution = .fillEqually
        tutView.spacing = 0


        return tutView
    }()
    override func viewDidLoad() {

        view.backgroundColor = .white

        view.addSubview(scrollView)
        scrollView.addSubview(parentScrollView)
        parentScrollView.addSubview(tutorialView)

        scrollView.widthAnchor.constraint(equalTo:self.view.widthAnchor,multiplier: 0.9).isActive = true
        scrollView.heightAnchor.constraint(equalTo:self.view.heightAnchor,multiplier: 0.7).isActive = true
        scrollView.centerXAnchor.constraint(equalTo: self.view.centerXAnchor).isActive = true
        scrollView.topAnchor.constraint(equalTo: self.view.topAnchor, constant: 75).isActive = true

        tutorialView.topAnchor.constraint(equalTo: parentScrollView.topAnchor,constant: +10).isActive = true}

似乎当我使用 topAnchor 添加最后一个 constraight 时,我得到了一个空白屏幕。当我把它取下来时,我实际上得到了两个 View 之间的边界,并且我可以进行一些滚动。 任何帮助,将不胜感激。谢谢

最佳答案

你给scrollView赋值了吗?

 uiScrollView.contentSize.width = 1200

然后给tutorialView设置与其他约束相同的宽度约束。

 NSLayoutConstraint.activate([
     tutorialView.widthAnchor.constraint(equalToConstant: 1200),
     tutorialView.heightAnchor.constraint(equalTo: uiScrollView.heightAnchor)
       ])

关于ios - 尝试以编程方式将 UIScrollView 添加到我的应用程序中,但我不断收到空白 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59975270/

相关文章:

objective-c - 调试时是否可以跳转到循环的特定迭代?

swift 。 RawRepresentable init 带有可选的 RawValue

ios - 根据属性值过滤数组

ios - 在分离的 View Controller 上呈现 View Controller

objective-c - iOS 崩溃日志 : what is 'purgeable_ptr_in_use_enumerator' ?

ios - 添加 NSArray 图像后出现问题

iphone - 向 Facebook 和 Twitter iOS 5 和 6 的简单帖子

ios - UNNotificationCategory 子类初始化问题

iOS 透明 uitabbar 不起作用

ios - 架构 arm64 的 undefined symbol : "_OBJC_CLASS_$_GANTracker