我是ios developpement的新手。我正在尝试在UIWebview中心显示pdf.i在mainbundle中有一个pdf。这是我的PDF在加载到UIWebView时的样子:(我放入一个棕色渐变作为UIWebView的背景,你可以看到我的意思更好)。
- (void)viewDidLoad
{
UIWebView *theWebView = [[UIWebView alloc] initWithFrame:[self.view bounds]];
[theWebView setContentMode:UIViewContentModeScaleAspectFit];
[theWebView setAutoresizingMask:UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth];
[theWebView setScalesPageToFit:YES];
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"test" ofType:@"pdf"];
NSURL *filePathURL = [NSURL fileURLWithPath:filePath];
[theWebView loadRequest:[NSURLRequest requestWithURL:filePathURL]];
[self.view addSubview:theWebView];
[super viewDidLoad];
}
我想将其置于中心位置,以使下边距与上边距相同。我如何使用PDF(本地)?
答案 0 :(得分:1)
CGRect rect = [[UIScreen mainScreen] bounds];
CGSize screenSize = rect.size;
UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(0,0,screenSize.width,screenSize.height)];
NSString *path = [[NSBundle mainBundle] pathForResource:@"pdf" ofType:@"pdf"];
NSURL *targetURL = [NSURL fileURLWithPath:path];
NSURLRequest *request = [NSURLRequest requestWithURL:targetURL];
[webView loadRequest:request];
[self.view addSubview:webView];