在UIView上添加类

时间:2018-01-15 23:53:20

标签: ios uiviewcontroller swift4

所以我一直以编程方式编写我的应用程序并遇到了这个问题......任何人都可以看到为什么我的标签和文本视图不是我期望它们的位置(在导航栏下方和青色UIView上) )。

enter image description here

我创建此代码的代码如下:     class AdminNewsController:UIViewController,UIPickerViewDelegate,UIPickerViewDataSource {

let mainView = UIView()
let picker = UIPickerView()

override func loadView() {

    view = UIView()
    view.frame = UIScreen.main.bounds
    view.backgroundColor = AMA_Constants.AppPrimaryColor

}

override func viewDidLoad() {
    super.viewDidLoad()

    navigationController?.navigationBar.barStyle = .black
    navigationController?.navigationBar.tintColor = AMA_Constants.AppSecondaryColor
    self.navigationItem.title = "Admin - News"

    newsCategoryPicker.delegate = self
    newsCategoryPicker.dataSource = self
    newsCategoryPicker.isHidden = true

    view.addSubview(mainView)
    setupMainView()

}

func setupMainView() {

    //mainView.backgroundColor = AMA_Constants.AppSecondaryColor
    mainView.backgroundColor = UIColor.cyan
    view.backgroundColor = UIColor.purple
    mainView.translatesAutoresizingMaskIntoConstraints = false

    if #available(iOS 11, *) {
        mainView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor).isActive = true
    } else {
        mainView.topAnchor.constraint(equalTo: view.topAnchor, constant: 50).isActive = true
    }
    mainView.leftAnchor.constraint(equalTo: view.leftAnchor).isActive = true
    mainView.widthAnchor.constraint(equalTo: view.widthAnchor).isActive = true
    mainView.heightAnchor.constraint(equalTo: view.heightAnchor, multiplier: 1/2).isActive = true

    addViews()

}


private func addViews() {
    mainView.addSubview(newsItemHeading)
    mainView.addSubview(newsTitleTextEdit)
    mainView.addSubview(newsSubTitleTextEdit)
    mainView.addSubview(newsCategoryTextEdit)
    //mainView.addSubview(newsCategoryPicker)
    mainView.addSubview(newsBodyTextEdit)
    mainView.addSubview(newsImageView)
    //view.addSubview(newsImageSelectButton)
    mainView.addSubview(newsSubmitButton)
    mainView.addSubview(newsResetButton)

}

所有约束都发生在此之后,每个项目都带头:

        newsItemHeading.anchor(mainView.topAnchor, left: mainView.leftAnchor, bottom: nil, right: nil, topConstant: AMA_Constants.Alignment_Space.PaddingVerticalExternal, leftConstant: AMA_Constants.Alignment_Space.PaddingHorizontalExternal, bottomConstant: 0, rightConstant: 0, widthConstant: 0, heightConstant: 0)

这是www.letsbuildthatapp.com的包装。

任何人都可以看到我在纠正错误方面出错了吗?

非常感谢

0 个答案:

没有答案