У меня есть два viewcontroller, у которых viewcontroller1 имеет обратный отсчет NSTimer до 0 и присутствует viewcontroller2.
Viewcontroller2 имеет wkwebview, и я также хочу добавить на него UIButton.
Поскольку я хочу создать кнопку закрытияотклонить viewcontroller2.
Я пытаюсь добавить UIButton в wkwebview, но не могу контролировать его NSlayoutConstrain.
Что не так с моим кодом?
Спасибо.
красная кнопка находится справа внизу.
// ViewController1.m
-(void)timerFired {
NSLog(@"===) self.count : %d", self.count);
if (self.count == 0) {
[self.timer invalidate];
self.timer = nil;
ViewController2 *vc = [[ViewController2 alloc] init];
[self presentViewController:vc animated:YES completion:nil];
} else {
self.count -= 1;
}
}
// ViewController2.m
#import "ViewController2.h"
#import <WebKit/WebKit.h>
@interface ViewController2 ()<WKScriptMessageHandler, WKNavigationDelegate,WKUIDelegate>
@property (nonatomic,strong) WKWebView* webView;
@property (nonatomic,strong) UIButton* button;
@property (nonatomic, strong) WKWebViewConfiguration * webConfig;
@end
@implementation ViewController2
- (void)viewDidLoad {
[super viewDidLoad];
self.webView = [[WKWebView alloc] initWithFrame:self.view.frame configuration:[self createWKWebApp]];
[self.view addSubview: self.webView];
[self.webView.configuration.preferences setValue:@YES forKey:@"allowFileAccessFromFileURLs"];
self.webView.scrollView.bounces = NO;
[self.webView setContentMode:UIViewContentModeScaleAspectFit];
self.webView.navigationDelegate = self;
self.webView.UIDelegate = self;
NSURL *url = [NSURL URLWithString:@"https://stackoverflow.com/"];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[self.webView loadRequest:request];
self.button = [UIButton buttonWithType: UIButtonTypeCustom];
self.button.translatesAutoresizingMaskIntoConstraints = false;
self.button.backgroundColor = UIColor.redColor;
[self.button setTitle:@"CLOSE" forState:UIControlStateNormal];
[self.button addTarget:self action:@selector(btnClicked:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:self.button];
NSLayoutConstraint *btnRight = [NSLayoutConstraint constraintWithItem:self.button attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeRight multiplier:1.0 constant:-20.0];
NSLayoutConstraint *btnTop = [NSLayoutConstraint constraintWithItem:self.button attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeBottom multiplier:1.0 constant:20.0];
NSLayoutConstraint *btnWidth = [NSLayoutConstraint constraintWithItem:self.button attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeWidth multiplier:1.0 constant:30.0];
NSLayoutConstraint *btnHeight = [NSLayoutConstraint constraintWithItem:self.button attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeHeight multiplier:1.0 constant:30.0];
[self.view addConstraints:@[btnRight, btnTop, btnWidth, btnHeight]];
}
- (void)btnClicked:(UIButton *)sender {
NSLog(@"BTN CLICKED");
[self dismissViewControllerAnimated:false completion:nil];
}
- (WKWebViewConfiguration *)createWKWebApp {
WKWebViewConfiguration *config = [[WKWebViewConfiguration alloc] init];
WKUserContentController *userContent = [[WKUserContentController alloc] init];
config.userContentController = userContent;
return config;
}
- (void)userContentController:(WKUserContentController*)userContentController didReceiveScriptMessage:(WKScriptMessage*)message {
NSString *name = message.name;
NSLog(@"===) message name = %@",name);
NSLog(@"===) body class = %@",[message.body class]);
}