0

我使用 UITextView 在 UIView 中显示文本,我需要文本块在此​​视图内垂直对齐。

为此,我使用了我在网上找到的一种观察者方法,它在大多数情况下都运行良好:

在 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)
    }
}

我试图在 unwind 中添加观察者,但它没有效果。我在这里想念什么吗?

谢谢,

4

1 回答 1

0

好的,我找到了。对于那些需要回答这个问题的人,只需在dismissViewController 完成块中添加您需要的任何调整。

@IBAction func prepareForUnwind(segue: UIStoryboardSegue) {
    if(segue.identifier == "CloseMenuSegue"){
        self.dismissViewControllerAnimated(true, completion: {
          //your code here
        })
    }
}
于 2015-09-06T20:02:47.450 回答