UITableviewController 与可编辑 UIWebView 的 contentInset 混淆
UITableviewController messes with contentInset of editable UIWebView
我有一个包含多个部分的 UITableViewController,是动态创建的。每个部分的其中一行包含一个 UIWebView 作为它的 accessoryView。 UIWebView 预填充了 HTML 以允许编辑其中的文本。当点击 UIWebView 时,tableView 会滚动,这样包含 UIWebView 的行就会在键盘上方;键盘出现,UIWebView 成为第一响应者。如果 UIWebView 位于 tableView 的顶部,则此方法工作正常,因此 table 不需要滚动。但是,如果点击下方的行以便 table 必须滚动,则 UIWebView 的 scrollView 的 contentInset 会发生变化。具体来说就是添加了contentInset的bottom值,让UIWebView变长变细而不是矩形。
我已上传屏幕截图以显示此内容:
选择 UIWebView 之前:
选择 UIWebView 后:
UIWebView退出第一响应者状态,键盘关闭后,returns恢复正常
我知道正在调整的是 contentInset 属性,因为我使用 keyValue "contentInset" 添加了 webView 的 scrollView 的观察者,我可以观察到它的变化,甚至记录它并确认它已更改。
原来问题不是contentInset,而是UIWebView内置的UIScrollView的contentOffset。这是我修复它的方法:
@interface SomeViewController ()
@property BOOL monitorContentOffsetChanges;
@end
@implementation SomeViewController
- (void)webViewCreationMethod {
UIWebView *myWebView = [[UIWebView alloc] init];
//Setup webView
//Observe any changes to the scrollView's contentOffset property
[[webView scrollView] addObserver:self forKeyPath:@"contentOffset" options:0 context:NULL];
[self setMonitorContentOffsetChanges:YES];
}
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
//The webView's contentOffset was changed
if (object == [[self webView] scrollView] && [keyPath isEqualToString:@"contentOffset"] && [self monitorContentOffsetChanges]) {
//Stop responding to changes in the contentOffset to prevent recursive calling of this method
[self setMonitorContentOffsetChanges:NO];
//Force the contentOffset back to zero
[[[self webView] scrollView] setContentOffset:CGPointZero animated:NO];
//Start monitoring changes to the contentOffset again in case of future changes
[self setMonitorContentOffsetChanges:YES];
}
}
@end
此解决方案是必需的,因为 UIWebView 的 scrollView 属性 是只读的,因此您不能将 UIScrollView 子类化并重写 setContentOffset 以不采取任何操作并将其强制放入 UIWebView。
我现在唯一不确定的是这个解决方案是否是 App Store 安全的。
我有一个包含多个部分的 UITableViewController,是动态创建的。每个部分的其中一行包含一个 UIWebView 作为它的 accessoryView。 UIWebView 预填充了 HTML 以允许编辑其中的文本。当点击 UIWebView 时,tableView 会滚动,这样包含 UIWebView 的行就会在键盘上方;键盘出现,UIWebView 成为第一响应者。如果 UIWebView 位于 tableView 的顶部,则此方法工作正常,因此 table 不需要滚动。但是,如果点击下方的行以便 table 必须滚动,则 UIWebView 的 scrollView 的 contentInset 会发生变化。具体来说就是添加了contentInset的bottom值,让UIWebView变长变细而不是矩形。
我已上传屏幕截图以显示此内容:
选择 UIWebView 之前:
选择 UIWebView 后:
UIWebView退出第一响应者状态,键盘关闭后,returns恢复正常
我知道正在调整的是 contentInset 属性,因为我使用 keyValue "contentInset" 添加了 webView 的 scrollView 的观察者,我可以观察到它的变化,甚至记录它并确认它已更改。
原来问题不是contentInset,而是UIWebView内置的UIScrollView的contentOffset。这是我修复它的方法:
@interface SomeViewController ()
@property BOOL monitorContentOffsetChanges;
@end
@implementation SomeViewController
- (void)webViewCreationMethod {
UIWebView *myWebView = [[UIWebView alloc] init];
//Setup webView
//Observe any changes to the scrollView's contentOffset property
[[webView scrollView] addObserver:self forKeyPath:@"contentOffset" options:0 context:NULL];
[self setMonitorContentOffsetChanges:YES];
}
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
//The webView's contentOffset was changed
if (object == [[self webView] scrollView] && [keyPath isEqualToString:@"contentOffset"] && [self monitorContentOffsetChanges]) {
//Stop responding to changes in the contentOffset to prevent recursive calling of this method
[self setMonitorContentOffsetChanges:NO];
//Force the contentOffset back to zero
[[[self webView] scrollView] setContentOffset:CGPointZero animated:NO];
//Start monitoring changes to the contentOffset again in case of future changes
[self setMonitorContentOffsetChanges:YES];
}
}
@end
此解决方案是必需的,因为 UIWebView 的 scrollView 属性 是只读的,因此您不能将 UIScrollView 子类化并重写 setContentOffset 以不采取任何操作并将其强制放入 UIWebView。
我现在唯一不确定的是这个解决方案是否是 App Store 安全的。