UIScrollview高度变化基于Uiwebview内容

时间:2014-03-09 08:21:05

标签: ios uiwebview uiscrollview

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

我在故事板中使用自动布局,目标是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

故事板的截图:

enter image description here

1 个答案:

答案 0 :(得分:1)

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

enter image description here

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

修改 请参阅this这可能会有所帮助的问题。