我有一个UIView
的视图控制器。在UIView
内部,我添加了文本标签等元素。当我运行我的代码时,它不存在。
import UIKit
class EventDetailViewController: UIViewController {
//variables that will hold data sent in through previous event controller
var eventImage = ""
var eventName = ""
var eventDescription = ""
var eventStreet = ""
var eventCity = ""
var eventState = ""
var eventZip = 0
var eventDate = ""
//
lazy var currentEventImage : UIImageView = {
let currentEvent = UIImageView()
let imageURL = URL(string: self.eventImage)
currentEvent.af_setImage(withURL: imageURL!)
currentEvent.clipsToBounds = true
currentEvent.translatesAutoresizingMaskIntoConstraints = false
currentEvent.contentMode = .scaleToFill
currentEvent.layer.masksToBounds = true
return currentEvent
}()
//will be responsible for creating the UIView that contains relevant event information
let eventInfoContainerView: UIView = {
let infoContainer = UIView()
infoContainer.backgroundColor = UIColor.white
infoContainer.layer.masksToBounds = true
return infoContainer
}()
lazy var eventNameLabel: UILabel = {
let currentEventName = UILabel()
currentEventName.text = self.eventName
currentEventName.translatesAutoresizingMaskIntoConstraints = false
return currentEventName
}()
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = UIColor.white
navigationItem.title = eventName
self.navigationItem.hidesBackButton = true
let backButton = UIBarButtonItem(image: UIImage(named: "icons8-Back-64"), style: .plain, target: self, action: #selector(GoBack))
self.navigationItem.leftBarButtonItem = backButton
//Subviews will be added here
view.addSubview(currentEventImage)
view.addSubview(eventInfoContainerView)
eventInfoContainerView.addSubview(eventNameLabel)
//Constraints will be added here
_ = currentEventImage.anchor(view.centerYAnchor, left: nil, bottom: nil, right: nil, topConstant: -305, leftConstant: 0, bottomConstant: 0, rightConstant: 0, widthConstant: self.view.frame.width, heightConstant: 200)
_ = eventInfoContainerView.anchor(currentEventImage.bottomAnchor, left: view.leftAnchor, bottom: view.bottomAnchor, right: view.rightAnchor, topConstant: 0, leftConstant: 0, bottomConstant: 0, rightConstant: 0, widthConstant: 0, heightConstant: 0)
_ = eventNameLabel.anchor(eventInfoContainerView.bottomAnchor, left: view.leftAnchor, bottom: nil, right: nil, topConstant: 20, leftConstant: 32, bottomConstant: 0, rightConstant: 0, widthConstant: 0, heightConstant: 0)
}
func GoBack(){
_ = self.navigationController?.popViewController(animated: true)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
我有点迷茫我看了一个教程,他们以类似的方式向UIView
添加元素并且出现了一些东西。任何见解都会有所帮助。尝试改变多个东西而没有真正让它出现,我已经研究了几个答案,我知道它可能是一个很小的东西,我的眼睛只是看着过去,但我现在无法发现它。
这是我设置约束的自定义方法
import UIKit
extension UIView {
func anchorToTop(_ top: NSLayoutYAxisAnchor? = nil, left: NSLayoutXAxisAnchor? = nil, bottom: NSLayoutYAxisAnchor? = nil, right: NSLayoutXAxisAnchor? = nil) {
anchorWithConstantsToTop(top, left: left, bottom: bottom, right: right, topConstant: 0, leftConstant: 0, bottomConstant: 0, rightConstant: 0)
}
func anchorWithConstantsToTop(_ top: NSLayoutYAxisAnchor? = nil, left: NSLayoutXAxisAnchor? = nil, bottom: NSLayoutYAxisAnchor? = nil, right: NSLayoutXAxisAnchor? = nil, topConstant: CGFloat = 0, leftConstant: CGFloat = 0, bottomConstant: CGFloat = 0, rightConstant: CGFloat = 0) {
translatesAutoresizingMaskIntoConstraints = false
if let top = top {
topAnchor.constraint(equalTo: top, constant: topConstant).isActive = true
}
if let bottom = bottom {
bottomAnchor.constraint(equalTo: bottom, constant: -bottomConstant).isActive = true
}
if let left = left {
leftAnchor.constraint(equalTo: left, constant: leftConstant).isActive = true
}
if let right = right {
rightAnchor.constraint(equalTo: right, constant: -rightConstant).isActive = true
}
}
func anchor(_ top: NSLayoutYAxisAnchor? = nil, left: NSLayoutXAxisAnchor? = nil, bottom: NSLayoutYAxisAnchor? = nil, right: NSLayoutXAxisAnchor? = nil, topConstant: CGFloat = 0, leftConstant: CGFloat = 0, bottomConstant: CGFloat = 0, rightConstant: CGFloat = 0, widthConstant: CGFloat = 0, heightConstant: CGFloat = 0) -> [NSLayoutConstraint] {
translatesAutoresizingMaskIntoConstraints = false
var anchors = [NSLayoutConstraint]()
if let top = top {
anchors.append(topAnchor.constraint(equalTo: top, constant: topConstant))
}
if let left = left {
anchors.append(leftAnchor.constraint(equalTo: left, constant: leftConstant))
}
if let bottom = bottom {
anchors.append(bottomAnchor.constraint(equalTo: bottom, constant: -bottomConstant))
}
if let right = right {
anchors.append(rightAnchor.constraint(equalTo: right, constant: -rightConstant))
}
if widthConstant > 0 {
anchors.append(widthAnchor.constraint(equalToConstant: widthConstant))
}
if heightConstant > 0 {
anchors.append(heightAnchor.constraint(equalToConstant: heightConstant))
}
anchors.forEach({$0.isActive = true})
return anchors
}
}
答案 0 :(得分:0)
好的 - 现在我们看到了您的.anchor(...)
功能......
标签的TOP设置为eventInfoContainerView
的底部topConstant: 20
... +20
将该值更改为-20
(假设您希望视图底部有标签):
_ = eventNameLabel.anchor(eventInfoContainerView.bottomAnchor, left: view.leftAnchor, bottom: nil, right: nil, topConstant: -20, leftConstant: 32, bottomConstant: 0, rightConstant: 0, widthConstant: 0, heightConstant: 0)
假设您的 .anchor(...)
功能/扩展功能正常,我认为唯一不对的是您忘了设置:
infoContainer.translatesAutoresizingMaskIntoConstraints = false
初始化 eventInfoContainerView