2017-08-08 108 views
1

我有一个视图控制器与UIView。在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

我没有答案,但是,我发现Xcode的视图调试功能方便这种事情。当它显示你不期望的东西时,拍摄屏幕快照。然后,您可以查看层次结构,约束条件,离屏视图和其他线索。 –

+0

我做了UIView在那里,但标签不是。当我检查文本标签中的变量时,它被存储在那里。但没有外观 – SJackson5

+0

@ SJackson5 - 你需要显示你的'.anchor(...)'功能。如果我们不知道该功能在做什么,我们就看不到会发生什么。 – DonMag

回答

0

OK - 现在我们看到你.anchor(...)功能...

您的标签顶部设置为eventInfoContainerViewtopConstant: 20底部。 ..这是

改变该值来-20(假设ÿ OU要沿着视图底部的标签):

_ = 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

+0

我的自定义方法已经在我的锚定函数中处理过了。查看编辑 – SJackson5

+0

@ SJackson5 - 看我的编辑 – DonMag

+0

还是没有 – SJackson5