Ahad Porkar
Ahad Porkar

Reputation: 1708

UIScrollview height change base on Uiwebview Contents

Im trying to set height of UIScrollView base on my UIWebView Content.

Im using autolayout in story board and target are ios7.

i know how to hard code size into height of UIScrollView by this code :

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

and also how to set UIWebView height base on contents :

- (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];
}

but dont know how to set the height of scrollview after setting the height of UIWebView

this is total code :

@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

screenshot of storyboard :

enter image description here

Upvotes: 1

Views: 535

Answers (1)

Rashad
Rashad

Reputation: 11197

If you are using autolayout then the resizing of UIScrollview height will not be effective. Turn autolayout off from IB and then try to set the height of UIScrollView.

enter image description here

Uncheck the option and then try to resize the scrollview. Hope this helps. :)

EDIT: See this question this may help.

Upvotes: 1

Related Questions