2015-09-06 114 views
0

我使用UITextView在UIView中顯示文本,我需要文本塊在此​​視圖內垂直對齊。從Unwind垂直對齊UITextView Segue Swift

要做到這一點我用了一個觀察者方法我在網上找到和它的工作大部分時間還算不錯:

在viewDidLoad中

moodPhrase.addObserver(self, forKeyPath: "contentSize", options: NSKeyValueObservingOptions.New, context: nil) 

然後觀察者:

//Update phrase content position (vertical alignment) 
    override func observeValueForKeyPath(keyPath: String, ofObject object: AnyObject, change: [NSObject : AnyObject], context: UnsafeMutablePointer<Void>) { 

     var topCorrect : CGFloat = (moodPhrase.frame.height - moodPhrase.contentSize.height); 
     topCorrect = topCorrect < 0.0 ? 0.0 : topCorrect/2 
     moodPhrase.contentOffset = CGPoint(x: 0, y: -topCorrect) 

    } 

我的問題是我有一個菜單控制器彈出此控制器,我正在使用unwind segue從菜單返回到UITextView控制器。當這樣做時,文本塊會返回到屏幕的頂部。

@IBAction func prepareForUnwind(segue: UIStoryboardSegue) { 
    if(segue.identifier == "CloseMenuSegue"){ 
     self.dismissViewControllerAnimated(true, completion: nil) 
    } 
} 

我試圖在放鬆裏面加上觀察者,但它沒有效果。我在這裏想念什麼?

謝謝,

回答

0

好的,我找到了。對於那些需要這個答案的人,只需在dismissViewController完成塊中添加你需要的任何調整即可。

@IBAction func prepareForUnwind(segue: UIStoryboardSegue) { 
    if(segue.identifier == "CloseMenuSegue"){ 
     self.dismissViewControllerAnimated(true, completion: { 
      //your code here 
     }) 
    } 
}