Objective-C 中的 UIScrollView 内具有动态高度的 UIWebView
UIWebView with dynamic height inside a UIScrollView in Objective-C
我正在尝试使用 "dynamic screen height" 制作一个 UIWebView。这个 webview 在 UIScrollView 里面,在其他组件下面。查看布局(它使用自动布局):
我的想法是只提供一个滚动条(来自 UIScrollView - 它正在工作)并且使 WebView 视口根据内容大小增长。而且它不起作用。
我为此做了什么:
- UIWebView 属性 "scale page to fit " 已开启;
- UIWebView 未分页;
- UIWebView 不允许用户交互。
在我的 UIViewController 中:
- (void) viewDidAppear:(BOOL)animated {
NSString *urlString = @"MY URL GOES HERE";
NSURL *url = [NSURL URLWithString:urlString];
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];
[_webViewDescription loadRequest:urlRequest];
_webViewDescription.delegate = self;
_webViewDescription.scrollView.scrollEnabled = NO;
_webViewDescription.scrollView.bounces = NO;
}
我的 UIWebViewDelegate:
- (void)webViewDidFinishLoad:(UIWebView *)webView {
CGRect frame = webView.frame;
frame.size.height = 1;
webView.frame = frame;
CGSize fittingSize = [webView sizeThatFits:CGSizeZero];
frame.size = fittingSize;
webView.frame = frame;
NSLog(@"size: %f, %f", fittingSize.width, fittingSize.height);
_myScrollView.contentSize = webView.bounds.size;
}
当我 运行 代码时,它会打印正确的 UIScrollView 大小和 UIWebView 大小。 UIScrollView 的高度变大了,但是 UIWebView 保持了第一次加载时的高度。
我正在真实设备上进行测试。
向您的 webView 添加一个高度限制,并制作一个类似
的 IBOutlet
@property(strong, nonatomic) IBOutlet NSLayoutConstraint *webViewHeightConstraint;
加载您的网络视图并在网络视图中委托
- (void)webViewDidFinishLoad:(UIWebView *)webView
获取 webview 内容高度并设置 webViewHeightConstraint.constant
如下所示:-
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
NSString *str = [webView stringByEvaluatingJavaScriptFromString:@"(document.height !== undefined) ? document.height : document.body.offsetHeight;"];
CGFloat height = str.floatValue;
webViewHeightConstraint.constant = height;
}
希望对您有所帮助。
尝试在viewDidLayoutSubviews()中设置webView.frame = frame;
您可以使用webView.scrollView.contentSize.height
获取webview内容的内容大小。
并且您可以使用此 webView.scrollView.contentSize.height
在 webViewDidFinishLoad
方法中设置滚动视图的 contentSize
。
在webViewDidFinishLoad
方法中像这样
[objWebView setFrame:CGRectMake(objWebView.frame.origin.x, objWebView.frame.origin.y, objWebView.frame.size.width, webView.scrollView.contentSize.height)];
if(objWebView.frame.origin.y+objWebView.frame.size.height > objScrollView.contentSize.height) {
[objScrollView setContentSize:CGSizeMake(objScrollView.frame.size.width, objWebView.frame.origin.y+objWebView.frame.size.height)];
}
为UIWebView设置动态高度,需要在webViewDidFinishLoadView方法中设置高度
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
CGFloat height = [[webview stringByEvaluatingJavaScriptFromString:@"document.height"] floatValue];
//For Width
//CGFloat width = [[webview stringByEvaluatingJavaScriptFromString:@"document.width"] floatValue];
CGRect frame = webview1.frame;
frame.size.height = height;
//frame.size.width = width; //Width
webview.frame = frame;
CGRect screenBounds = [UIScreen mainScreen].bounds ;
CGFloat widthForIpad = CGRectGetWidth(screenBounds) ;
CGFloat heightForIpad = CGRectGetHeight(screenBounds) ;
NSLog(@"The iPad width is - %fl",widthForIpad);
NSLog(@"The iPad height is - %fl",heightForIpad);
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
{
if ([[UIScreen mainScreen] bounds].size.height == 568)
scrollView.contentSize = CGSizeMake(320, height+370); //set your required height
else
scrollView.contentSize = CGSizeMake(320, height+350); //set your required height
}
else
{
if ([[UIScreen mainScreen] bounds].size.height == 1024)
scrollView.contentSize = CGSizeMake(320, height+370); //For iPad
}
}
我正在尝试使用 "dynamic screen height" 制作一个 UIWebView。这个 webview 在 UIScrollView 里面,在其他组件下面。查看布局(它使用自动布局):
我的想法是只提供一个滚动条(来自 UIScrollView - 它正在工作)并且使 WebView 视口根据内容大小增长。而且它不起作用。
我为此做了什么:
- UIWebView 属性 "scale page to fit " 已开启;
- UIWebView 未分页;
- UIWebView 不允许用户交互。
在我的 UIViewController 中:
- (void) viewDidAppear:(BOOL)animated {
NSString *urlString = @"MY URL GOES HERE";
NSURL *url = [NSURL URLWithString:urlString];
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];
[_webViewDescription loadRequest:urlRequest];
_webViewDescription.delegate = self;
_webViewDescription.scrollView.scrollEnabled = NO;
_webViewDescription.scrollView.bounces = NO;
}
我的 UIWebViewDelegate:
- (void)webViewDidFinishLoad:(UIWebView *)webView {
CGRect frame = webView.frame;
frame.size.height = 1;
webView.frame = frame;
CGSize fittingSize = [webView sizeThatFits:CGSizeZero];
frame.size = fittingSize;
webView.frame = frame;
NSLog(@"size: %f, %f", fittingSize.width, fittingSize.height);
_myScrollView.contentSize = webView.bounds.size;
}
当我 运行 代码时,它会打印正确的 UIScrollView 大小和 UIWebView 大小。 UIScrollView 的高度变大了,但是 UIWebView 保持了第一次加载时的高度。
我正在真实设备上进行测试。
向您的 webView 添加一个高度限制,并制作一个类似
的 IBOutlet@property(strong, nonatomic) IBOutlet NSLayoutConstraint *webViewHeightConstraint;
加载您的网络视图并在网络视图中委托
- (void)webViewDidFinishLoad:(UIWebView *)webView
获取 webview 内容高度并设置 webViewHeightConstraint.constant
如下所示:-
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
NSString *str = [webView stringByEvaluatingJavaScriptFromString:@"(document.height !== undefined) ? document.height : document.body.offsetHeight;"];
CGFloat height = str.floatValue;
webViewHeightConstraint.constant = height;
}
希望对您有所帮助。
尝试在viewDidLayoutSubviews()中设置webView.frame = frame;
您可以使用webView.scrollView.contentSize.height
获取webview内容的内容大小。
并且您可以使用此 webView.scrollView.contentSize.height
在 webViewDidFinishLoad
方法中设置滚动视图的 contentSize
。
在webViewDidFinishLoad
方法中像这样
[objWebView setFrame:CGRectMake(objWebView.frame.origin.x, objWebView.frame.origin.y, objWebView.frame.size.width, webView.scrollView.contentSize.height)];
if(objWebView.frame.origin.y+objWebView.frame.size.height > objScrollView.contentSize.height) {
[objScrollView setContentSize:CGSizeMake(objScrollView.frame.size.width, objWebView.frame.origin.y+objWebView.frame.size.height)];
}
为UIWebView设置动态高度,需要在webViewDidFinishLoadView方法中设置高度
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
CGFloat height = [[webview stringByEvaluatingJavaScriptFromString:@"document.height"] floatValue];
//For Width
//CGFloat width = [[webview stringByEvaluatingJavaScriptFromString:@"document.width"] floatValue];
CGRect frame = webview1.frame;
frame.size.height = height;
//frame.size.width = width; //Width
webview.frame = frame;
CGRect screenBounds = [UIScreen mainScreen].bounds ;
CGFloat widthForIpad = CGRectGetWidth(screenBounds) ;
CGFloat heightForIpad = CGRectGetHeight(screenBounds) ;
NSLog(@"The iPad width is - %fl",widthForIpad);
NSLog(@"The iPad height is - %fl",heightForIpad);
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
{
if ([[UIScreen mainScreen] bounds].size.height == 568)
scrollView.contentSize = CGSizeMake(320, height+370); //set your required height
else
scrollView.contentSize = CGSizeMake(320, height+350); //set your required height
}
else
{
if ([[UIScreen mainScreen] bounds].size.height == 1024)
scrollView.contentSize = CGSizeMake(320, height+370); //For iPad
}
}