add 添加原生标题栏

This commit is contained in:
zher52 2020-09-18 18:13:24 +08:00
parent 031d07d47b
commit 7fa4336a77
4 changed files with 66 additions and 26 deletions

View File

@ -22,6 +22,10 @@
@property (nonatomic, assign) BOOL location;
@property (nonatomic, assign) BOOL statusbar;
@property (nonatomic, assign) BOOL titlebar;
@property (nonatomic, copy) NSString* color;
@property (nonatomic, copy) NSString* background;
@property (nonatomic, assign) BOOL backbutton;
@property (nonatomic, assign) BOOL toolbar;
@property (nonatomic, copy) NSString* closebuttoncaption;
@property (nonatomic, copy) NSString* closebuttoncolor;

View File

@ -27,7 +27,9 @@
// default values
self.location = YES;
self.toolbar = YES;
self.statusbar = YES;
self.titlebar = NO;
self.color = nil;
self.background = nil;
self.closebuttoncaption = nil;
self.toolbarposition = @"bottom";
self.cleardata = NO;

View File

@ -70,6 +70,7 @@
@property (nonatomic, strong) IBOutlet UIBarButtonItem* forwardButton;
@property (nonatomic, strong) IBOutlet UIActivityIndicatorView* spinner;
@property (nonatomic, strong) IBOutlet UIToolbar* toolbar;
@property (nonatomic, strong) IBOutlet UINavigationBar*titlebar;
@property (nonatomic, strong) IBOutlet CDVWKInAppBrowserUIDelegate* webViewUIDelegate;
@property (nonatomic, weak) id <CDVScreenOrientationDelegate> orientationDelegate;

View File

@ -309,26 +309,10 @@ static CDVWKInAppBrowser* instance = nil;
// Run later to avoid the "took a long time" log message.
dispatch_async(dispatch_get_main_queue(), ^{
if (weakSelf.inAppBrowserViewController != nil) {
// float osVersion = [[[UIDevice currentDevice] systemVersion] floatValue];
// __strong __typeof(weakSelf) strongSelf = weakSelf;
// if (!strongSelf->tmpWindow) {
// CGRect frame = [[UIScreen mainScreen] bounds];
// if(initHidden && osVersion < 11){
// frame.origin.x = -10000;
// }
// strongSelf->tmpWindow = [[UIWindow alloc] initWithFrame:frame];
// }
// UIViewController *tmpController = [[UIViewController alloc] init];
//
// [strongSelf->tmpWindow setRootViewController:tmpController];
// [strongSelf->tmpWindow setWindowLevel:UIWindowLevelNormal];
//
// if(!initHidden || osVersion < 11){
// [self->tmpWindow makeKeyAndVisible];
// }
// [tmpController presentViewController:nav animated:!noAnimate completion:nil];
self.inAppBrowserViewController.view.frame = CGRectMake(0,self.statusbarHieght,self.inAppBrowserViewController.view.frame.size.width,self.inAppBrowserViewController.view.frame.size.height - 10);
/* */
self.inAppBrowserViewController.view.frame = CGRectMake(0,self.statusbarHieght,self.inAppBrowserViewController.view.frame.size.width,self.inAppBrowserViewController.view.frame.size.height - self.statusbarHieght/2);
[self.viewController.view addSubview:self.inAppBrowserViewController.view];
/* */
}
});
}
@ -918,6 +902,53 @@ BOOL isExiting = FALSE;
} else {
[self.toolbar setItems:@[self.closeButton, flexibleSpaceButton, self.backButton, fixedSpaceButton, self.forwardButton]];
}
//
self.titlebar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 44)];
UIColor *backgroundColor = nil;
if(_browserOptions.background == nil) {
backgroundColor = [UIColor whiteColor];
}else{
backgroundColor = [self colorFromHexString:_browserOptions.background];
}
UIColor *fontColor = nil;
if(_browserOptions.color == nil) {
fontColor = [UIColor blackColor];
}else{
fontColor = [self colorFromHexString:_browserOptions.color];
}
self.titlebar.barTintColor = backgroundColor;
self.titlebar.tintColor =fontColor;
self.titlebar.translucent = YES;
UINavigationItem *navigationItem = [[UINavigationItem alloc] initWithTitle:nil];
NSDictionary *barButtonAppearanceDict = @{NSFontAttributeName : [UIFont fontWithName:@"Symbol" size:24], NSForegroundColorAttributeName: fontColor};
if(_browserOptions.backbutton) {
UIBarButtonItem *leftButton = [[UIBarButtonItem alloc] initWithTitle: @""
style:UIBarButtonItemStylePlain
target:self
action:@selector(goBack:)];
leftButton.tintColor = fontColor;
[leftButton setTitleTextAttributes:barButtonAppearanceDict forState:UIControlStateNormal];
[navigationItem setLeftBarButtonItem:leftButton];
}
UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithTitle:@"⊗"
style:UIBarButtonItemStyleDone
target:self
action:@selector(close)];
[rightButton setTitleTextAttributes:barButtonAppearanceDict forState:UIControlStateNormal];
rightButton.tintColor = fontColor;
//
[navigationItem setTitle:self.webView.title];
//
[self.titlebar pushNavigationItem:navigationItem animated:NO];
//
[navigationItem setRightBarButtonItem:rightButton];
self.titlebar.hidden = !_browserOptions.titlebar;
[self.view addSubview:self.titlebar];
//
self.view.backgroundColor = [UIColor grayColor];
[self.view addSubview:self.toolbar];
@ -1098,9 +1129,6 @@ BOOL isExiting = FALSE;
return NO;
}
- (void)newClose{
}
- (void)close
{
self.currentURL = nil;
@ -1132,7 +1160,11 @@ BOOL isExiting = FALSE;
- (void)goBack:(id)sender
{
[self.webView goBack];
if(self.webView.canGoBack){
[self.webView goBack];
} else {
[self close];
}
}
- (void)goForward:(id)sender
@ -1145,8 +1177,9 @@ BOOL isExiting = FALSE;
if (IsAtLeastiOSVersion(@"7.0") && !viewRenderedAtLeastOnce) {
viewRenderedAtLeastOnce = TRUE;
CGRect viewBounds = [self.webView bounds];
viewBounds.origin.y = self.statusbarHieght;
viewBounds.size.height = viewBounds.size.height - self.statusbarHieght;
int offsetY = _browserOptions.titlebar? 44 : 0 ;
viewBounds.origin.y = offsetY ;
viewBounds.size.height = viewBounds.size.height - offsetY;
self.webView.frame = viewBounds;
[[UIApplication sharedApplication] setStatusBarStyle:[self preferredStatusBarStyle]];
}