2016-02-03 155 views
0

我想在圖像視圖頂部添加一個帶有文本「原始」的小疊加視圖,該視圖應該只在用戶查看原始圖像時可見。設置約束後,子視圖無法顯示

這裏是我的toggleImage功能,當用戶按下它時,圖像視圖將顯示原始圖像(並且會彈出一個文本爲「原始」的視圖),然後在用戶舉手時回到過濾圖像。

func toggleImage(sender: UILongPressGestureRecognizer) { 
    sender.minimumPressDuration = 0.3 
    if sender.state == .Began{ 
     imageView.image = self.rawImage! 

     view.addSubview(originalText) 


    }else if sender.state == .Ended{ 
     imageView.image = self.newImage 
    } 

現在我想爲文本視圖設置一個約束。

func toggleImage(sender: UILongPressGestureRecognizer) { 
    sender.minimumPressDuration = 0.3 
    if sender.state == .Began{ 
     imageView.image = self.rawImage! 

     view.addSubview(originalText) 

     originalText.translatesAutoresizingMaskIntoConstraints = false 

     let bottomConstraint = originalText.bottomAnchor.constraintEqualToAnchor(view.topAnchor) 
     let leftConstriant = originalText.leftAnchor.constraintEqualToAnchor(view.leftAnchor) 
     let rightConstraint = originalText.rightAnchor.constraintEqualToAnchor(view.rightAnchor) 
     let heightConstraint = originalText.heightAnchor.constraintEqualToConstant(44) 

     NSLayoutConstraint.activateConstraints([bottomConstraint,leftConstriant,rightConstraint,heightConstraint]) 
     view.layoutIfNeeded() 



    }else if sender.state == .Ended{ 
     imageView.image = self.newImage 
    } 

subView現在無法顯示,請問如何解決這個問題?

這裏是未設置約束條件時模擬器的屏幕截圖。 screenshot

回答

0

你有底部等於頂部:

let bottomConstraint = originalText.bottomAnchor.constraintEqualToAnchor(view.topAnchor) 

您的看法是最有可能被渲染關閉屏幕/視圖的邊界之外。

將其更改爲:

let topConstraint = originalText.topAnchor.constraintEqualToAnchor(view.topAnchor) 
相關問題