html page vertical cutoff picture of code 2如何让wkwebview(目标c)显示加载的html文件的所有内容,而不是垂直切断页面的一半? (xcode 8.2.1) 系统10.11.6 目标ios 8.0及以上
并且我不是最好的代码,所以如果有什么我可以学习的东西,我会在我去的时候应用它....
这是Xcode8.2.1的viewcontrollerm代码,我使用uiwebview和webview没有像wkwebview一样流畅地拾取本地html文件。我尝试了很多不同的解决方案,显然没有帮助......而且很简单...我想要做的就是加载index.html,我有在html和css中编写的java,以及一些图像文件夹的链接。如果有人可以帮助它将是金色的,许多祝福... < / p>
另外在上面发布的照片我正在使用iPhone 6的ios模拟器,你可以看到文字的底部段被切断了,我只能滚动...它不会让我滚动任何比那。我相信它与自动布局有关,使高度尺寸固定,不会再滚动html。我可能完全错了,但如果有人有任何建议让我知道......
其他图片是项目其余部分的代码。如果有任何帮助。
#import "ViewController.h"
#import <WebKit/WebKit.h>
@interface ViewController () <WKScriptMessageHandler, WKNavigationDelegate>
@property (weak, nonatomic) IBOutlet UIButton *callJavascriptButton;
@property (strong, nonatomic) WKWebView *webView;
@property (strong, nonatomic) UIButton *scriptButton;
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// WKWebView
WKWebViewConfiguration *configuration = [[WKWebViewConfiguration alloc] init];
WKUserContentController *controller = [[WKUserContentController alloc] init];
// Add addScriptMessageHandler in javascript:
window.webkit.messageHandlers.MyObserver.postMessage()
[controller addScriptMessageHandler:self name:@"MyObserver"];
configuration.userContentController = controller;
[self.view addSubview:self.webView];
// baseURL needs to be set for local files to load correctly
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"index"
ofType:@"html"];
NSString *html = [NSString stringWithContentsOfFile:filePath
encoding:NSUTF8StringEncoding error:nil];
[self.webView loadHTMLString:html baseURL:[NSBundle mainBundle].resourceURL];
}
- (IBAction)callJavascriptTapped:(id)sender
{
NSString *script = @"indexJS()";
[self.webView evaluateJavaScript:script completionHandler:^(NSString *result,
NSError *error) {
if (error) {
NSLog(@"evaluateJavaScript error:%@", [error
localizedDescription]);
} else {
NSLog(@"evaluateJavaScript result:%@", result);
}
}];
}
- (void)userContentController:(WKUserContentController *)userContentController
didReceiveScriptMessage:(WKScriptMessage *)message
{
}
- (void)webView:(WKWebView *)webView didFinishNavigation:(null_unspecified
WKNavigation *)navigation;
{
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
答案 0 :(得分:0)
查看Apple的Swift手册(在iBooks中),了解自动布局的工作原理。从viewDidLoad或viewWillAppear(你需要调整顶部)调用的这样的东西应该可以工作:
目标-C:
- (void) example {
WKWebView *webView = [[WKWebView alloc] initWithFrame: CGRectZero];
webView.translatesAutoresizingMaskIntoConstraints = NO;
[self.view addSubview: webView];
[webView.leftAnchor constraintEqualToAnchor: self.view.leftAnchor].active = YES;
[webView.rightAnchor constraintEqualToAnchor: self.view.rightAnchor].active = YES;
[webView.centerXAnchor constraintEqualToAnchor: self.view.centerXAnchor].active = YES;
[webView.topAnchor constraintEqualToAnchor: self.view.topAnchor].active = YES;
[webView.bottomAnchor constraintEqualToAnchor: self.view.bottomAnchor].active = YES;
}
斯威夫特:
func example() {
let webView = WKWebView(frame: .zero)
webView.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(webView)
webView.leftAnchor.constraint(equalTo: view.leftAnchor).isActive = true
webView.rightAnchor.constraint(equalTo: view.rightAnchor).isActive = true
webView.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
webView.topAnchor.constraint(equalTo: view.topAnchor).isActive = true
webView.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true
}