【问题标题】:How to create Onboarding\Walkthrough swift如何快速创建 Onboarding\Walkthrough
【发布时间】:2023-03-28 02:55:01
【问题描述】:

我正在尝试为第一次使用的用户创建一个欢迎入职,但没有一个视图在模拟器中闲逛,我得到的只是当 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, width: pageView.frame.size.width-20, height: pageView.frame.size.height - 60 - 130 - 15))
            
            let button = UIButton(frame: CGRect(x: 10, y: pageView.frame.size.height - 60, width: pageView.frame.size.width-20, 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) {
        
    }

}

【问题讨论】:

    标签: ios swift model-view-controller uiviewcontroller


    【解决方案1】:

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

    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)
    }
    

    【讨论】:

    • 添加并更改帧大小后,我仍然在控制台中收到相同的消息
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-13
    • 2015-06-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-11
    相关资源
    最近更新 更多