2017-08-08 11 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(...)関数を参照してくださいことを...あなたのラベルの

TOPはtopConstant: 20eventInfoContainerViewのBOTTOMに設定されています。 .. Yを仮定(-20+20

変更すること値でありますOU)は、ビューの下部に沿ってラベルをしたい:あなたの.anchor(...)機能/拡張を想定し

_ = eventNameLabel.anchor(eventInfoContainerView.bottomAnchor, left: view.leftAnchor, bottom: nil, right: nil, topConstant: -20, leftConstant: 32, bottomConstant: 0, rightConstant: 0, widthConstant: 0, heightConstant: 0) 

は私が間違っている唯一のことは、あなたが設定し忘れたということだと思います、正常に動作している:

infoContainer.translatesAutoresizingMaskIntoConstraints = false 

eventInfoContainerView

+0

私のカスタムメソッドはすでに私のアンカー関数でこれを処理。編集を参照 – SJackson5

+0

@ SJackson5 - 私の編集を参照してください – DonMag

+0

まだありません – SJackson5

関連する問題