如何创建入职\演练快速

问题描述

我正在尝试为初次使用的用户创建一个欢迎使用的入门软件,但是模拟器中没有任何视图出现,当显示onboardingVC时,我得到的只是一个红色背景。谁能看到为什么标题,按钮和图像不显示的问题?

这是我在控制台中收到的消息:

警告:尝试在视图不在窗口层次结构中的上显示! 找到全部!!

let holderView: UIView = {
    let view = UIView()
        view.translatesAutoresizingMaskIntoConstraints = true
        view.backgroundColor = .darkGray
        return view
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = UIColor.white
    }
    
    override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()
        view.backgroundColor = UIColor.red
        configure()
    }
    
    private func configure() {
        let scrollView = UIScrollView(frame: holderView.bounds)
        holderView.addSubview(scrollView)
        
        let titles = ["Hi","Welcome","real nigga"]
        
        for x in 0..<3 {
            let pageView = UIView(frame: CGRect(x: CGFloat(x) * holderView.frame.size.width,y: 0,width: holderView.frame.size.width,height: holderView.frame.size.height))
            scrollView.addSubview(pageView)
            
            let label = UILabel(frame: CGRect(x: 10,y: 10,width: pageView.frame.size.width-20,height: 120))
            
            let imageView = UIImageView(frame: CGRect(x: 10,y: 10+120+10,height: pageView.frame.size.height - 60 - 130 - 15))
            
            let button = UIButton(frame: CGRect(x: 10,y: pageView.frame.size.height - 60,height: 50))
            
            label.textAlignment = .center
            label.font = UIFont.systemFont(ofSize: 16,weight: .semibold)
            pageView.addSubview(label)
            label.text = titles[x]
            
            imageView.contentMode = .scaleAspectFill
            imageView.image = UIImage(named: "BankCard\(x)")
            pageView.addSubview(imageView)
            
            button.setTitleColor(.red,for: .normal)
            button.backgroundColor = .black
            button.setTitle("Continue",for: .normal)
            if x == 2 {
                button.setTitle("Get Started",for: .normal)
            }
            button.addTarget(self,action: #selector(didTapButton),for: .touchUpInside)
            pageView.addSubview(button)

        }
    }
    
    @objc func didTapButton(_ button: UIButton) {
        
    }

}

解决方法

“其视图不在窗口层次结构中” 您没有将创建的视图添加到主视图中,请尝试使用此视图将子视图添加到主视图中。

self.view.addSubview(holderView)

也不要忘记像这样为持有人视图添加框架

UIView(frame: CGRect(x: 0,y: 0,width: UIScreen.main.bounds.width,height: UIScreen.main.bounds.height))
    view.translatesAutoresizingMaskIntoConstraints = true

并且在视图内部确实加载

   override func viewDidLoad() {
    super.viewDidLoad()
    view.backgroundColor = UIColor.white
    self.view.addSubview(holderView)
}
,

除了@Moumen的回答,您的holderView框架也是零原因。

   let holderView: UIView = {
    let view = UIView() // Frame is (x:0,y:0,width :0,height :0)
   ...}

即使将此视图添加到层​​次结构中,也不会看到holderView,因为帧为零

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...