【问题标题】:Create UIWebView programmatically以编程方式创建 UIWebView
【发布时间】:2012-09-17 04:12:35
【问题描述】:

我已经尝试了一段时间,但我没有做对。

我在支持文件中编写了以下初始化函数:

- (id)initWithFrame:(CGRect)frame
 {
    self = [super initWithFrame:frame];
    if (self) {
    webview=[[UIWebView alloc]initWithFrame:CGRectMake(0, 0, 1024,768)];
 }
    return self;
}

并在 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]; 
}

我也尝试在appDelegate的didFinishLaunchingWithOptions:方法中创建webview,但也没成功。

正确的方法是什么?

【问题讨论】:

    标签: objective-c ios cocoa-touch uiwebview


    【解决方案1】:

    您似乎忘记将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;
    }
    

    另外viewDidLoad 不是创建子视图的正确位置。您应该将webview 公开为视图的属性,然后从viewDidLoad 访问它,如下所示:

    NSString *url=@"http://www.google.com";
    NSURL *nsurl=[NSURL URLWithString:url];
    NSURLRequest *nsrequest=[NSURLRequest requestWithURL:nsurl];
    [[self.view webview] loadRequest:nsrequest]; 
    

    【讨论】:

    • @user1153798 如果代码在UIViewControllerself.view 应该没问题。 .view 可能是错误的 - 请改用 [self.view webview](请参阅编辑)。
    【解决方案2】:

    我希望这个解决方案对你有所帮助。

    1. 根据您的问题,只需将这些行添加到 - (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];
    

    我使用了webView的静态框架,你可以根据你的要求使用。

    1. UIWebView 已弃用:在 iOS 12.0 中首次弃用 - 不再支持;请采纳WKWebView,下面是Objective C和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)
    

    目标 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];
    

    【讨论】:

    • 我用的是web view的静态框架,你可以根据你的要求使用。
    【解决方案3】:
    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];
    

    在viewcontroller的接口中声明UIWebviewDelegate

    【讨论】:

      【解决方案4】:

      这个答案比@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];
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-01-08
        • 2011-09-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-03-28
        相关资源
        最近更新 更多