来自 Unwind Segue 的垂直对齐 UITextView Swift
Vertical Align UITextView from Unwind Segue Swift
我使用 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中添加观察者,但没有效果。我在这里错过了什么吗?
谢谢,
好的,我找到了。对于那些需要答案的人,只需在 dismissViewController 完成块中添加您需要的任何调整。
@IBAction func prepareForUnwind(segue: UIStoryboardSegue) {
if(segue.identifier == "CloseMenuSegue"){
self.dismissViewControllerAnimated(true, completion: {
//your code here
})
}
}
我使用 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中添加观察者,但没有效果。我在这里错过了什么吗?
谢谢,
好的,我找到了。对于那些需要答案的人,只需在 dismissViewController 完成块中添加您需要的任何调整。
@IBAction func prepareForUnwind(segue: UIStoryboardSegue) {
if(segue.identifier == "CloseMenuSegue"){
self.dismissViewControllerAnimated(true, completion: {
//your code here
})
}
}