ios 以编程方式创建 UIWebView
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12586421/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Create UIWebView programmatically
提问by user1153798
I've been trying this for a while, but I'm not getting it right.
我已经尝试了一段时间,但我没有做对。
I have written the following init function in a supporting file:
我在支持文件中编写了以下 init 函数:
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
webview=[[UIWebView alloc]initWithFrame:CGRectMake(0, 0, 1024,768)];
}
return self;
}
and following in ViewController.m
并在 ViewController.m 中关注
- (void)viewDidLoad
{
[super viewDidLoad];
UIWebView *view = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 1024, 768)];
NSString *url=@"http://www.google.com";
NSURL *nsurl=[NSURL URLWithString:url];
NSURLRequest *nsrequest=[NSURLRequest requestWithURL:nsurl];
[view loadRequest:nsrequest];
}
I also tried creating the webview in didFinishLaunchingWithOptions:
method of appDelegate, but it also didn't work.
我也尝试在 appDelegate 的didFinishLaunchingWithOptions:
方法中创建 webview ,但它也不起作用。
What is the correct way?
什么是正确的方法?
回答by Ashvin Ajadiya
I hope this solution will help you.
我希望这个解决方案能帮助你。
- As per your problem, simply add these lines in
- (void)viewDidLoad
- 根据您的问题,只需将这些行添加到
- (void)viewDidLoad
UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 1024, 768)]; NSString *urlString = @"https://www.google.com"; NSURL *url = [NSURL URLWithString:urlString]; NSURLRequest *request = [NSURLRequest requestWithURL:url]; webView loadRequest:request]; [self.view addSubview:webView];
I have used the static frame of the webView, you can use according to your requirement.
UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 1024, 768)]; NSString *urlString = @"https://www.google.com"; NSURL *url = [NSURL URLWithString:urlString]; NSURLRequest *request = [NSURLRequest requestWithURL:url]; webView loadRequest:request]; [self.view addSubview:webView];
webView的静态框架我用过,大家可以根据自己的需求使用。
- But
UIWebView
is deprecated: first deprecated in iOS 12.0 - No longer supported; please adoptWKWebView
, so updated code for Objective C and Swift are below
- 但
UIWebView
已弃用:首先在 iOS 12.0 中弃用 - 不再支持;请采纳WKWebView
,因此目标 C 和 Swift 的更新代码如下
Swift:
迅速:
import WebKit
let theConfiguration = WKWebViewConfiguration()
let webView = WKWebView(frame: view.frame, configuration: theConfiguration)
let nsurl = URL(string: "https://www.google.com")
var nsrequest: URLRequest? = nil
if let nsurl = nsurl {
nsrequest = URLRequest(url: nsurl)
}
if let nsrequest = nsrequest {
webView.load(nsrequest)
}
view.addSubview(webView)
Objective C:
目标 C:
#import <WebKit/WKWebView.h>
#import <WebKit/WKWebViewConfiguration.h>
WKWebViewConfiguration *theConfiguration = [[WKWebViewConfiguration alloc] init];
WKWebView *webView = [[WKWebView alloc] initWithFrame:self.view.frame configuration:theConfiguration];
NSURL *nsurl=[NSURL URLWithString:@"https://www.google.com"];
NSURLRequest *nsrequest=[NSURLRequest requestWithURL:nsurl];
[webView loadRequest:nsrequest];
[self.view addSubview:webView];
回答by dasblinkenlight
It looks like you have forgotten to add webview
as a subview of its parent view:
看起来您忘记添加webview
为其父视图的子视图:
-(id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
webview=[[UIWebView alloc]initWithFrame:CGRectMake(0, 0, 1024,768)];
[self addSubview:webview];
}
return self;
}
Also viewDidLoad
is not the right place to create subviews. You should expose webview
as a property of your view, and then access it from viewDidLoad
, like this:
也viewDidLoad
就是不创建子视图正确的地方。您应该将其webview
作为视图的属性公开,然后从 访问它viewDidLoad
,如下所示:
NSString *url=@"http://www.google.com";
NSURL *nsurl=[NSURL URLWithString:url];
NSURLRequest *nsrequest=[NSURLRequest requestWithURL:nsurl];
[[self.view webview] loadRequest:nsrequest];
回答by Muzahidul Islam
UIWebView *webView = [[UIWebView alloc]initWithFrame:CGRectMake(0, 0, 320, 568)];
webView.delegate = self;
NSString *url=@"http://www.google.com";
NSURL *nsurl=[NSURL URLWithString:url];
NSURLRequest *nsrequest=[NSURLRequest requestWithURL:nsurl];
[webview loadRequest:nsrequest];
[self.view addSubview:webview];
Declare UIWebviewDelegate in the interface of the viewcontroller
在viewcontroller的界面中声明UIWebviewDelegate
回答by BharathRao
This is answer is slight improvement over @Ashvin Ajadiya's answer. To dynamically set the width and height of the web view according to your device use this code inside viewWillAppearor else it won't get proper device width and height
这个答案比@Ashvin Ajadiya 的答案略有改进。要根据您的设备动态设置 Web 视图的宽度和高度,请在viewWillAppear 中使用此代码,否则将无法获得正确的设备宽度和高度
-(void)viewWillAppear:(BOOL)animated
{
UIWebView *webview=[[UIWebView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width,self.view.frame.size.height)];
NSString *url=@"http://www.google.com";
NSURL *nsurl=[NSURL URLWithString:url];
NSURLRequest *nsrequest=[NSURLRequest requestWithURL:nsurl];
[webview loadRequest:nsrequest];
[self.view addSubview:webview];
}