gpt4 book ai didi

ios - 如何在WKWebview上添加UIButton?

转载 作者:行者123 更新时间:2023-11-29 05:23:22 33 4
gpt4 key购买 nike

我有两个 View Controller ,其中 View Controller 1的NSTimer倒计时为0并显示 View Controller 2。
viewcontroller2有一个wkwebview,我还想在上面添加UIButton。
因为我想创建一个关闭按钮来关闭 viewcontroller2。
我尝试在 wkwebview 上添加 UIButton,但我无法控制它的 NSlayoutConstrain。
我的代码有什么问题?
谢谢。

enter image description here

red button is on right bottom.

//  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]);

}

最佳答案

你应该知道这样的约束

item1.attribute1 = multiplier × item2.attribute2 + constant

所以正确的约束常数应该是-20

NSLayoutConstraint *btnRight = [NSLayoutConstraint constraintWithItem:self.button attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeRight multiplier:1.0 constant:-20.0];

关于ios - 如何在WKWebview上添加UIButton?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58443587/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com