UIScrollview 高度变化基于 Uiwebview 内容

Posted

技术标签:

【中文标题】UIScrollview 高度变化基于 Uiwebview 内容【英文标题】:UIScrollview height change base on Uiwebview Contents 【发布时间】:2014-03-09 08:21:04 【问题描述】:

我试图根据我的UIWebView 内容设置UIScrollView 的高度。

我在故事板中使用自动布局,目标是 ios7。

我知道如何通过此代码将大小硬编码为 UIScrollView 的高度:

-(void) viewDidLayoutSubviews
     UIScrollView *sv = (UIScrollView *)[self.view viewWithTag:30];
    sv.contentSize = CGSizeMake(0,1200);

以及如何根据内容设置UIWebView 高度:

- (void)webViewDidStartLoad:(UIWebView *)bweb

    CGRect frame = byweb.frame;

    frame.size.height = 5.0f;

    bweb.frame = frame;

-(void) viewDidAppear:(BOOL)animated

    [super viewDidAppear:animated];


- (void)webViewDidFinishLoad:(UIWebView *)bweb
    CGSize mWebViewTextSize = [bweb sizeThatFits:CGSizeMake(1.0f, 1.0f)];
    CGRect mWebViewFrame = bweb.frame;
    mWebViewFrame.size.height = mWebViewTextSize.height;
    self.cellhieghtforwebview = &(mWebViewFrame.size.height);
    bweb.frame = mWebViewFrame;
    bweb.scrollView.scrollEnabled = NO;
    bweb.scrollView.bounces = NO;
    UIScrollView *sv = (UIScrollView *)[self.view viewWithTag:30];
    sv.contentSize = CGSizeMake(0, *(self.cellhieghtforwebview));
    [self viewDidLayoutSubviews];

但是设置UIWebView的高度后不知道怎么设置scrollview的高度

这是总代码:

@interface NBViewController () <UIWebViewDelegate,UIScrollViewDelegate>
@property (nonatomic, strong) NSMutableDictionary *imageDownloadsInProgress;
@end


@implementation NBViewController

- (void)viewDidLoad


    [super viewDidLoad];
    self.imageDownloadsInProgress = [NSMutableDictionary dictionary];
    CGFloat xOffset = 0;
    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) 
        xOffset = 224;
    

    pullRightView = [[PullableView alloc] initWithFrame:CGRectMake(0, 200, 200, 300)];
    pullRightView.backgroundColor = [UIColor lightGrayColor];
    pullRightView.openedCenter = CGPointMake(100, 200);
    pullRightView.closedCenter = CGPointMake(-70, 200);
    pullRightView.center = pullRightView.closedCenter;
    pullRightView.animate = NO;

    pullRightView.handleView.backgroundColor = [UIColor darkGrayColor];
    pullRightView.handleView.frame = CGRectMake(170, 0, 30, 300);

    [self.view addSubview:pullRightView];

    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 160, 30)];
    label.backgroundColor = [UIColor darkGrayColor];
    label.textColor = [UIColor whiteColor];
    label.text = @"User name";
    label.transform = CGAffineTransformMakeRotation(-M_PI_2);
    label.center = CGPointMake(185, 80);

    [pullRightView addSubview:label];

    NSString *s = self.Bodys;
    s =  [@"<html dir=\"rtl\">" stringByAppendingString:s];
    s = [s stringByAppendingString:@"</html>"];
    UIWebView *bweb = (UIWebView *)[self.view viewWithTag:3];
    UIImageView *uimg = (UIImageView *)[self.view viewWithTag:20];
    UILabel *title = (UILabel *)[self.view viewWithTag:2];

    uimg.image = self.NBimage;
    [title setText:self.Titles];
    bweb.delegate = self;
    [bweb loadHTMLString:s baseURL:nil];
    bweb.scrollView.scrollEnabled = NO;
    bweb.scrollView.bounces = NO;

-(void) viewDidLayoutSubviews
     UIScrollView *sv = (UIScrollView *)[self.view viewWithTag:30];
    if((self.cellhieghtforwebview != nil))
        NSLog(@"not null enter %d" , (int)*(self.cellhieghtforwebview));
        if((int)*(self.cellhieghtforwebview) > 0)
        NSLog(@"above zero enter %d" , (int)*(self.cellhieghtforwebview));

     sv.contentSize = CGSizeMake(0,(int)*(self.cellhieghtforwebview));
            
    else
        NSLog(@"aaaa %d" ,  1200);
    sv.contentSize = CGSizeMake(0,1200);
    

- (void)didReceiveMemoryWarning

    [super didReceiveMemoryWarning];

    NSArray *allDownloads = [self.imageDownloadsInProgress allValues];
    [allDownloads makeObjectsPerformSelector:@selector(cancelDownload)];
    [self.imageDownloadsInProgress removeAllObjects];


- (void)webViewDidStartLoad:(UIWebView *)bweb

    CGRect frame = bweb.frame;

    frame.size.height = 5.0f;

    bweb.frame = frame;

-(void) viewDidAppear:(BOOL)animated

    [super viewDidAppear:animated];

- (void)webViewDidFinishLoad:(UIWebView *)bweb
    NSLog(@"finish loading");
    CGSize mWebViewTextSize = [bweb sizeThatFits:CGSizeMake(1.0f, 1.0f)]; 
    CGRect mWebViewFrame = bweb.frame;
    mWebViewFrame.size.height = mWebViewTextSize.height;
    self.cellhieghtforwebview = &(mWebViewFrame.size.height);
    bweb.frame = mWebViewFrame;
    NSLog(@"%f",*self.cellhieghtforwebview);
    bweb.scrollView.scrollEnabled = NO;
    bweb.scrollView.bounces = NO;
    UIScrollView *sv = (UIScrollView *)[self.view viewWithTag:30];
    sv.contentSize = CGSizeMake(0, *(self.cellhieghtforwebview));
    [self viewDidLayoutSubviews];


-(void)bweb:(UIWebView *)bweb didFailLoadWithError:(NSError *)error 
   NSLog(@"error loading");


@end

故事板截图:

【问题讨论】:

您在 UIScrollView 上有其他视图还是在 UIScrollView 上只有 UIWebView? 是的,我还有另外两个视图(uiimageview,标签)。它们都在 uiscrollview 里面。 您是在 webview 下方还是上方添加图像视图和标签?您能否制作一张图片并附上,以便我了解您的视图应该是什么样子? 确定我添加了故事板的屏幕截图 【参考方案1】:

如果您使用自动布局,则 UIScrollview 高度的调整大小将无效。从 IB 关闭自动布局,然后尝试设置 UIScrollView 的高度。

取消选中该选项,然后尝试调整滚动视图的大小。希望这可以帮助。 :)

编辑: 请参阅this 问题,这可能会有所帮助。

【讨论】:

感谢回答问题是我想使用自动布局功能。这就是我使用“viewDidLayoutSubviews”的原因。

以上是关于UIScrollview 高度变化基于 Uiwebview 内容的主要内容,如果未能解决你的问题,请参考以下文章

如何根据视图高度值的变化上下移动UIScrollView?

调整 UITableView 高度(嵌入在 UIScrollView 中)

将动态高度设置为 UIScrollView Swift

UIStackView:从xib加载视图和更新subView的高度约束没有反映任何变化?

UIScrollView 高度不会根据视图大小动态更新?

动画 UICollectionView 的高度变化