Here is my final solution ( hope it is useful for others ):
In Storyboard, I added a UIScrollView
in the view controller, and link it with the IBOutlet
.
Header file:
#import <UIKit/UIKit.h>
#import "GDataXMLNode.h"
@interface ViewController : UIViewController<UIWebViewDelegate> {
IBOutlet UIScrollView *scroll_view;
}
@property (nonatomic, strong) UIWebView *web_view;
@end
Implementation file:
float currentY;
NSArray *array;
int count;
GDataXMLDocument *doc;
- (void)viewDidLoad
{
[super viewDidLoad];
currentY = 0;
count = 0;
[self loadXML];
}
- (void)loadXML {
// Some codes to load the XML contents into array variable
[self loadWebView];
}
- (void)loadWebView {
if(count < array.count) {
GDataXMLElement *post = [array objectAtIndex:count];
NSString *msg = [[[post elementsForName:@"message"] objectAtIndex:0] stringValue];
count++;
self.web_view = [[UIWebView alloc] initWithFrame:CGRectMake(10, currentY, 300, 1.0f)];
self.web_view.delegate = self;
[self.web_view setHidden:YES];
[self.web_view loadHTMLString:msg baseURL:nil];
[scroll_view addSubview:self.web_view];
} else {
// end the process
[scroll_view setContentSize:CGSizeMake(320, currentY + 30)];
return;
}
}
- (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;
[webView setHidden:NO];
currentY += webView.frame.size.height + 20.0f; // 20.0f is spaces between posts
NSLog(@"WebView Height: %.1f", webView.frame.size.height);
[self loadWebView];
}