UIWebView:使框架适合视图大小,允许在 UISplitViewController 上的该框架内滚动
这个问题快把我逼疯了。
我有一个 UIWebView,我试图填充 UISplitViewController 右侧的完整视图:
CGSize boundsSize = self.view.bounds.size;
self.webView = [[[UIWebView alloc] initWithFrame:CGRectMake(0, 0, boundsSize.width, boundsSize.height)] autorelease];
self.webView.delegate = self;
self.webView.opaque = NO;
self.webView.backgroundColor = [UIColor redColor];
self.webView.allowsInlineMediaPlayback = YES;
self.webView.dataDetectorTypes = UIDataDetectorTypeAddress | UIDataDetectorTypeLink | UIDataDetectorTypePhoneNumber;
self.webView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleLeftMargin;
[self.view addSubview:webView];
首先,当我不知道方向时,如何使框架大小适合视图?我可以自动调整到合适的大小吗?
接下来,当我将 HTML 加载到 webView 中时,我希望框架大小保持不变(就像 UIScrollView 一样),但让新内容能够随该框架滚动。有些内容较长,有些则不然。所以,我尝试调整框架,但这只会使我的框架更大,而不是我的内容大小。
- (void)webViewDidFinishLoad:(UIWebView *)aWebView {
// Calculate the size of our webview
CGRect frame = aWebView.frame;
frame.size.height = 1;
aWebView.frame = frame;
CGSize fittingSize = [aWebView sizeThatFits:CGSizeZero];
frame.size = fittingSize;
aWebView.frame = frame;
}//end
这不可能吗?我是否需要将 webView 放入 UIScrollView 中,并在每次将其加载到 webView 中时将其 contentSize 设置为新内容的高度?
This problem is driving me nuts.
I have a UIWebView which I am trying to make fill the full view on the right side of a UISplitViewController:
CGSize boundsSize = self.view.bounds.size;
self.webView = [[[UIWebView alloc] initWithFrame:CGRectMake(0, 0, boundsSize.width, boundsSize.height)] autorelease];
self.webView.delegate = self;
self.webView.opaque = NO;
self.webView.backgroundColor = [UIColor redColor];
self.webView.allowsInlineMediaPlayback = YES;
self.webView.dataDetectorTypes = UIDataDetectorTypeAddress | UIDataDetectorTypeLink | UIDataDetectorTypePhoneNumber;
self.webView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleLeftMargin;
[self.view addSubview:webView];
First of all, how do I account for making the frame size fit the view when I don't know the orientation? Can I autoresize to the appropriate size?
Next, when I load my HTML into my webView I want the frame size to stay the same (just like a UIScrollView does), but have that new content be able to be scrolled with that frame. Some content is much longer, some is not. So, I have tried to adjust the frame, but this just makes my frame larger rather than my content size.
- (void)webViewDidFinishLoad:(UIWebView *)aWebView {
// Calculate the size of our webview
CGRect frame = aWebView.frame;
frame.size.height = 1;
aWebView.frame = frame;
CGSize fittingSize = [aWebView sizeThatFits:CGSizeZero];
frame.size = fittingSize;
aWebView.frame = frame;
}//end
Is this not possible? Do I need to put the webView inside of a UIScrollView and set the contentSize of that to the height of my new content every time I load it into my webView?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我认为这就是让这项工作成功所需的全部。
[articleWebView sizeThatFits:CGSizeZero];
I think that is all you need to make this work.
[articleWebView sizeThatFits:CGSizeZero];
看了一下自动调整大小蒙版。我认为这就是让这项工作成功所需的全部。
https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIView_Class/UIView/UIView.html#//apple_ref/doc/uid/TP40006816-CH3-SW6
Gave a look at the autoresize mask. I think that is all you need to make this work.
https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIView_Class/UIView/UIView.html#//apple_ref/doc/uid/TP40006816-CH3-SW6
似乎我必须在加载时手动调整框架:
Seems I had to manually adjust my frame on load: