implement iOS navbar custom left & right view

This commit is contained in:
王劲鹏 2020-01-10 17:40:43 +08:00 committed by osborn
parent 894f23cd5b
commit 26d32d788e
3 changed files with 82 additions and 0 deletions

View File

@ -103,5 +103,15 @@ - (void)doric_navBar_setBackgroundColor:(UIColor *)color {
[self.navigationController.navigationBar setBackgroundImage:UIImageWithColor(color) forBarMetrics:UIBarMetricsDefault]; [self.navigationController.navigationBar setBackgroundImage:UIImageWithColor(color) forBarMetrics:UIBarMetricsDefault];
} }
- (void)doric_navBar_setLeft:(UIView *)view {
UIBarButtonItem *custom = [[UIBarButtonItem alloc] initWithCustomView:view];
self.navigationItem.leftBarButtonItem = custom;
}
- (void)doric_navBar_setRight:(UIView *)view {
UIBarButtonItem *custom = [[UIBarButtonItem alloc] initWithCustomView:view];
self.navigationItem.rightBarButtonItem = custom;
}
@end @end

View File

@ -28,4 +28,8 @@
- (void)doric_navBar_setTitle:(NSString *)title; - (void)doric_navBar_setTitle:(NSString *)title;
- (void)doric_navBar_setBackgroundColor:(UIColor *)color; - (void)doric_navBar_setBackgroundColor:(UIColor *)color;
- (void)doric_navBar_setLeft:(UIView *)view;
- (void)doric_navBar_setRight:(UIView *)view;
@end @end

View File

@ -19,8 +19,14 @@
#import "DoricNavBarPlugin.h" #import "DoricNavBarPlugin.h"
#import "DoricUtil.h" #import "DoricUtil.h"
#import "DoricViewNode.h"
#import "DoricExtensions.h"
@implementation DoricNavBarPlugin @implementation DoricNavBarPlugin
static NSString *TYPE_LEFT = @"navbar_left";
static NSString *TYPE_RIGHT = @"navbar_right";
- (void)isHidden:(NSDictionary *)param withPromise:(DoricPromise *)promise { - (void)isHidden:(NSDictionary *)param withPromise:(DoricPromise *)promise {
if (self.doricContext.navBar) { if (self.doricContext.navBar) {
dispatch_async(dispatch_get_main_queue(), ^{ dispatch_async(dispatch_get_main_queue(), ^{
@ -65,4 +71,66 @@ - (void)setBgColor:(NSDictionary *)param withPromise:(DoricPromise *)promise {
} }
} }
- (void)setLeft:(NSDictionary *)params withPromise:(DoricPromise *)promise {
if (self.doricContext.navBar) {
dispatch_async(dispatch_get_main_queue(), ^{
NSString *viewId = params[@"id"];
NSString *type = params[@"type"];
DoricViewNode *viewNode = [self.doricContext targetViewNode:viewId];
if (!viewNode) {
viewNode = [[DoricViewNode create:self.doricContext withType:type] also:^(DoricViewNode *it) {
it.viewId = viewId;
[it initWithSuperNode:nil];
it.view.layoutConfig = [DoricLayoutConfig new];
[self.doricContext.navBar doric_navBar_setLeft:it.view];
NSMutableDictionary <NSString *, DoricViewNode *> *map = self.doricContext.headNodes[TYPE_LEFT];
if (map != nil) {
self.doricContext.headNodes[TYPE_LEFT][viewId] = it;
} else {
map = [[NSMutableDictionary alloc] init];
map[viewId] = it;
self.doricContext.headNodes[TYPE_LEFT] = map;
}
}];
}
[viewNode blend:params[@"props"]];
[promise resolve:nil];
});
} else {
[promise reject:@"Not implement NavBar"];
}
}
- (void)setRight:(NSDictionary *)params withPromise:(DoricPromise *)promise {
if (self.doricContext.navBar) {
dispatch_async(dispatch_get_main_queue(), ^{
NSString *viewId = params[@"id"];
NSString *type = params[@"type"];
DoricViewNode *viewNode = [self.doricContext targetViewNode:viewId];
if (!viewNode) {
viewNode = [[DoricViewNode create:self.doricContext withType:type] also:^(DoricViewNode *it) {
it.viewId = viewId;
[it initWithSuperNode:nil];
it.view.layoutConfig = [DoricLayoutConfig new];
[self.doricContext.navBar doric_navBar_setRight:it.view];
NSMutableDictionary <NSString *, DoricViewNode *> *map = self.doricContext.headNodes[TYPE_RIGHT];
if (map != nil) {
self.doricContext.headNodes[TYPE_RIGHT][viewId] = it;
} else {
map = [[NSMutableDictionary alloc] init];
map[viewId] = it;
self.doricContext.headNodes[TYPE_RIGHT] = map;
}
}];
}
[viewNode blend:params[@"props"]];
[promise resolve:nil];
});
} else {
[promise reject:@"Not implement NavBar"];
}
}
@end @end