【问题标题】:UIView subviews not appearingUIView 子视图没有出现
【发布时间】:2017-08-08 17:48:09
【问题描述】:

我有一个带有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
}

}

【问题讨论】:

  • 我没有答案,但是我发现 Xcode 的 View Debugging 功能对这类事情很方便。当屏幕显示您不期望的内容时拍摄屏幕快照。然后可以查看层次结构、约束、离屏视图等线索。
  • 我做了 UIView 存在但标签不存在。当我检查 textlabel 中的变量时,它存储在那里。但没有出现
  • @SJackson5 - 你需要展示你的.anchor(...) 函数。如果我们不知道该函数在做什么,我们就看不到会发生什么。
  • 您没有正确设置约束,您确定这是在编译,因为 UIView 没有名为 anchor 的方法吗?
  • @Abizern 那是我自己的自定义方法

标签: ios swift


【解决方案1】:

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

标签的 TOP 设置为 eventInfoContainerView 的 BOTTOM 和 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

【讨论】:

  • 我的自定义方法已经在我的锚函数中处理了这个问题。查看修改
  • 还是没有
  • 由于限制而损失了一个小时。我真诚地感谢帮助人
  • 但是,我必须将其设置为 -400 以将其放置在正确的位置,是否有任何关于为什么错误的提示?
  • 我认为它根本没有被添加到 uiview 中
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-01-14
  • 2021-02-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-05-16
  • 1970-01-01
相关资源
最近更新 更多