以编程方式添加的UIWebView不滚动

时间:2016-02-03 09:27:30

标签: scroll uiwebview

如果我使用故事板添加uiwebview,它的效果很好。但是以编程方式添加的网页浏览不会滚动。

self.webview=[[UIWebView alloc] init];
    path=[[NSBundle mainBundle] pathForResource:@"index" ofType:@"html"];
    [self.webview setFrame:CGRectMake(-200, 0, 200, 300)];
    [self.webview loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:path]]];
    [self.view addSubview:self.webview];

1 个答案:

答案 0 :(得分:0)

UIWebview使用以下代码以编程方式添加到View中:

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    UIWebView *webview=[[UIWebView alloc]initWithFrame:CGRectMake(self.view.frame.origin.x, self.view.frame.origin.y, self.view.frame.size.width,600)];
    [webview setBackgroundColor:[UIColor redColor]];
    NSString *url=@"http://www.facebook.com";
    NSURL *nsurl=[NSURL URLWithString:url];
    NSURLRequest *nsrequest=[NSURLRequest requestWithURL:nsurl];
    [webview loadRequest:nsrequest];

    [self.view addSubview:webview];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end