我正在尝试以编程方式实例化UIView和UIViewController,但是在显示任何标签,文本字段等时出现问题。
我的视图控制器如下所示:
import UIKit
class AdminNewsController: UIViewController {
var adminNewsView : AdminNewsView!
let newsTitleMarker : UILabel = {
let label = UILabel()
label.text = "Title: "
label.font.withSize(AMA_Constants.FontSizes.HeadingSize)
label.textColor = AMA_Constants.FontColor.LabelMarkingColor
label.textAlignment = .center
label.translatesAutoresizingMaskIntoConstraints = false
return label
}()
let newsTitleTextEdit : UITextField = {
let textEdit = UITextField()
textEdit.text = "Text View"
textEdit.textColor = AMA_Constants.FontColor.LabelMarkingColor
textEdit.textAlignment = .center
textEdit.translatesAutoresizingMaskIntoConstraints = false
return textEdit
}()
override func viewDidLoad() {
super.viewDidLoad()
adminNewsView = AdminNewsView()
adminNewsView.backgroundColor = AMA_Constants.AppPrimaryColor
self.view.addSubview(adminNewsView)
setupViews()
navigationController?.navigationBar.barStyle = .black
navigationController?.navigationBar.tintColor = AMA_Constants.AppSecondaryColor
self.navigationItem.title = "Admin - News"
}
func setupViews() {
adminNewsView.addSubview(newsTitleMarker)
adminNewsView.addSubview(newsTitleTextEdit)
adminNewsView.translatesAutoresizingMaskIntoConstraints = false
adminNewsView.leadingAnchor.constraint(equalTo: self.view.leadingAnchor).isActive = true
adminNewsView.topAnchor.constraint(equalTo: self.view.topAnchor).isActive = true
adminNewsView.widthAnchor.constraint(equalTo: self.view.widthAnchor).isActive = true
adminNewsView.heightAnchor.constraint(equalTo: self.view.heightAnchor).isActive = true
newsTitleMarker.anchor(adminNewsView.topAnchor, left: adminNewsView.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)
newsTitleTextEdit.anchor(newsTitleMarker.topAnchor, left: newsTitleMarker.rightAnchor, bottom: nil, right: adminNewsView.rightAnchor, topConstant: 0, leftConstant: AMA_Constants.Alignment_Space.paddingHorizontalInternal, bottomConstant: 0, rightConstant: AMA_Constants.Alignment_Space.paddingHorizontalExternal, widthConstant: 0, heightConstant: 0)
}}
但在模拟器中,视图是正确的(白色)颜色,但没有标签或UITextView。
问题:
答案 0 :(得分:0)
我的代码存在一些问题,因此我以一种我认为有助于更好地说明程序化视图控制器过程的方式重构它。其中一个问题是您从未创建过视图控制器的视图,这对于程序化视图控制器是必需的。
我强烈建议像从左到右,从上到下的书一样编写代码。首先声明需要成为实例属性的所有属性,然后调用所有生命周期事件(我不会将任何代码放入生命周期方法中,因为它读取的方法非常简单)然后写出这些方法。
import UIKit
class AdminNewsController: UIViewController {
// MARK: Properties
let adminNewsView = AdminNewsView()
let newsTitleMarker = UILabel()
let newsTitleTextEdit = UITextField()
// MARK: Lifecycle
override func loadView() {
// do NOT call super.loadView() when programmatically creating a
// view controller
// do all of your basic setup here in loadView
setView()
addAdminNewsView()
addNewsTitleMarker()
addNewsTitleTextEdit()
}
override func viewDidLoad() {
super.viewDidLoad()
// do call super.viewDidLoad() however
// do post-basic setup stuff here in viewDidLoad
// viewDidLoad is called after the entire view controller has been
// loaded into memory so consider that
}
// other lifecycle events that come after viewDidLoad where you can do some
// last-second touches include viewDidLayoutSubviews(), viewDidAppear(), etc.
deinit {
// do any cleanup here like deactivating timers, removing observers, etc.
}
// MARK: Methods
func setView() {
view = UIView()
view.frame = UIScreen.main.bounds
view.backgroundColor = UIColor.white
}
func addAdminNewsView() {
adminNewsView.backgroundColor = AMA_Constants.AppPrimaryColor
adminNewsView.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(adminNewsView)
adminNewsView.leadingAnchor.constraint(equalTo: view.leadingAnchor).isActive = true
adminNewsView.topAnchor.constraint(equalTo: view.topAnchor).isActive = true
adminNewsView.widthAnchor.constraint(equalTo: view.widthAnchor).isActive = true
adminNewsView.heightAnchor.constraint(equalTo: view.heightAnchor).isActive = true
}
func addNewsTitleMarker() {
newsTitleMarker.text = "Title: "
newsTitleMarker.font.withSize(AMA_Constants.FontSizes.HeadingSize)
newsTitleMarker.textColor = AMA_Constants.FontColor.LabelMarkingColor
newsTitleMarker.textAlignment = .center
newsTitleMarker.translatesAutoresizingMaskIntoConstraints = false
adminNewsView.addSubview(newsTitleMarker)
// add newsTitleMarker constraints
}
func addNewsTitleTextEdit() {
newsTitleTextEdit.text = "Text View"
newsTitleTextEdit.textColor = AMA_Constants.FontColor.LabelMarkingColor
newsTitleTextEdit.textAlignment = .center
newsTitleTextEdit.translatesAutoresizingMaskIntoConstraints = false
adminNewsView.addSubview(newsTitleTextEdit)
// add newsTitleTextEdit constraints
}
}
既然你问过,这是一个子类UIView
的样子。请注意不同的生命周期方法。
class SomeView: UIView {
// MARK: Properties
private let itemBanner = UIImageView()
private let itemLabel = UILabel()
var itemName: String
var itemPicture: UIImage
// MARK: Lifecycle
init(name: String, picture: UIImage) {
self.itemName = name
self.itemPicture = picture
super.init(frame: CGRect.zero)
config()
addItemBanner()
addItemLabel()
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func updateConstraints() {
super.updateConstraints()
setConstraints()
}
override func layoutSubviews() {
super.layoutSubviews()
// this can be a very useful method when subclassing views
}
// MARK: Methods
private func config() {
isOpaque = false
}
private func addItemBanner() {
itemBanner.image = itemPicture
itemBanner.clipsToBounds = true
itemBanner.contentMode = .scaleAspectFill
itemBanner.translatesAutoresizingMaskIntoConstraints = false
addSubview(itemBanner)
}
private func addItemLabel() {
itemLabel.text = itemName
itemLabel.textAlignment = .left
itemLabel.numberOfLines = 3
itemLabel.lineBreakMode = .byTruncatingTail
itemLabel.font = UIFont.bigTitle
itemLabel.translatesAutoresizingMaskIntoConstraints = false
itemBanner.addSubview(itemLabel)
}
private func setConstraints() {
// banner
itemBanner.centerXAnchor.constraint(equalTo: centerXAnchor).isActive = true
itemBanner.topAnchor.constraint(equalTo: topAnchor).isActive = true
itemBanner.widthAnchor.constraint(equalTo: widthAnchor).isActive = true
itemBanner.heightAnchor.constraint(equalToConstant: 96).isActive = true
itemBanner.bottomAnchor.constraint(equalTo: bottomAnchor).isActive = true
// label
itemLabel.centerXAnchor.constraint(equalTo: itemBanner.centerXAnchor).isActive = true
itemLabel.centerYAnchor.constraint(equalTo: itemBanner.centerYAnchor).isActive = true
itemLabel.widthAnchor.constraint(equalTo: itemBanner.widthAnchor, constant: -32).isActive = true
itemLabel.sizeToFit()
}
}