ios - UIView subview 未出现

标签 ios swift

我有一个带有 UIView 的 View Controller 。在 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
}

}

最佳答案

好的 - 现在我们看到了您的 .anchor(...) 函数...

标签的顶部设置为 eventInfoContainerView 的底部,topConstant: 20 ... +20

将该值更改为 -20(假设您想要 View 底部的标签):

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

关于ios - UIView subview 未出现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45574898/

相关文章:

iphone - 当动画结束时,保留动画的结束图像

ios - 调用 timeAgoSinceDate 函数 Swift

Swift 中的 JSON 和 Node 如何实现 GET 请求中类别的过滤器

ios - Firebase Storage 异步图像下载随机顺序

ios - [UIApplication sharedApplication].delegate 的转换

ios - Artefacts 在 Swift Xcode 中画线

json - 使用 Alamofire 获取 json 值

ios - 如何根据单元格文本获取UICollectionViewCell的indexPath?

swift - 数据不会从核心数据本地存储加载

iphone - NSDateFormatter返回空值dateFromString:iPhone?