简体   繁体   English

没有Storyboard的自定义UIView

[英]Custom UIView without Storyboard

Now I'm practicing build IOS app without using storyboard , but I have a problem want to solve , I created a custom UIView called BannerView and added a background(UIView) and a title(UILabel) , and called this BannerView in the MainVC , but run this app , it crashes at the function setupSubviews() and I don't know why. 现在,我在不使用Storyboard的情况下练习构建IOS应用程序,但是我有一个要解决的问题,我创建了一个名为BannerView的自定义UIView,并添加了一个background(UIView)和一个title(UILabel),并在BannerView中将其MainVCMainVC ,但是运行此应用程序,它将在函数setupSubviews()崩溃,我不知道为什么。

import UIKit
import SnapKit

class BannerView: UIView {

    var background: UIView!
    var title: UILabel!

    override init(frame: CGRect) {
        super.init(frame: frame)
        setupSubviews()
    }

    convenience init() {
        self.init(frame: CGRect.zero)
    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        setupSubviews()
    }

    func setupSubviews() {
        background.backgroundColor = .gray
        title.text = "INHEART"

        self.addSubview(background)
        self.addSubview(title)
    }

    override func layoutSubviews() {
        background.snp.makeConstraints { make in
            make.width.equalTo(ScreenWidth)
            make.height.equalTo(BannerHeight)
            make.left.top.right.equalTo(0)
        }

        title.snp.makeConstraints { make in
            make.width.equalTo(100)
            make.center.equalTo(background.snp.center)
        }
    }

}




class MainVC: UIViewController {

    var bannerView:BannerView!

    override func viewDidLoad() {
        super.viewDidLoad()
        bannerView = BannerView(frame: CGRect.zero)
        view.addSubview(bannerView)
    }

}

Your properties do not appear to be initialised 您的媒体资源似乎没有被初始化

var background: UIView!
var title: UILabel!

You should initialize these in your init method 您应该在init方法中初始化它们

self.background = UIView()
self.title = UILabel()

If you use force unwrapping on a class property you must initialize in the init method. 如果对类属性使用强制展开,则必须在init方法中进行初始化。 XCode should be complaining to you about this and your error message should show a similar error XCode应该就此向您抱怨,并且您的错误消息应显示类似的错误

You are not initialised the background view please initialised them 您尚未初始化背景视图,请对其进行初始化

self.background = UIView()
self.title = UILabel()

and if you want to create custom view by use of xib the follow them Custum View 如果您要使用xib创建自定义视图,请遵循它们Custum View

You must have to initialised the self.background = UIView() and self.title = UILabel() first. 您必须首先初始化self.background = UIView()和self.title = UILabel()。

You can initalised them in setupSubviews() function before the set/assign values to them. 您可以在将值设置/分配给它们之前,在setupSubviews()函数中初始化它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将UIViewController视图属性设置为不带故事板/笔尖的自定义UIView类 - Set UIViewController view property to custom UIView class without storyboard / nib 情节提要中的自定义UIView不显示子视图 - Custom UIView in storyboard not showing subviews 在情节提要中使用自定义uiview - use a custom uiview inside a storyboard 自定义UIView,在Storyboard上具有动态高度 - Custom UIView with dynamic height on Storyboard 在 Storyboard 中编辑自定义 UIView 子类 - Editing Custom UIView Subclasses in Storyboard 在Storyboard中设计的Custom UITableViewCell中的Custom UIView - Custom UIView in Custom UITableViewCell designed in Storyboard 有没有办法在故事板中“设计”自定义UIView,并在不添加任何特定控制器的情况下在多个ViewControllers中使用? - Is there a way to 'design' a custom UIView in storyboard, and use in multiple ViewControllers without adding to any specific controller? 自定义TableViewCell没有故事板 - Custom TableViewCell without storyboard 使用情节提要中的自定义UIView捕获视图,什么也不返回? - Capture view with custom UIView in storyboard returning nothing? 将参数从故事板传递到自定义UIView iOS - Pass parameter from storyboard to custom UIView iOS
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM