公司的产品要求做一个很通用框架的APP,在整个APP构思的过程中还是有很多误区,以下为一些初步总结:
一、UINavigationController+UITabBarController 和 UITabBarController +UINavigationController差别
UITabBarController +UINavigationController ,从ViewController1push到下一个ViewController,UITabBar会自动消失:
UITabBarController *tabbar = [[UITabBarController alloc] init];
ViewController1 *v1 = [[ViewController1 alloc] init];
[tabbar addChildViewController:v1];
ViewControllerB *vb = [[ViewControllerB alloc] init];
[tabbar addChildViewController:vb];
MeViewController *me = [[MeViewController alloc] init];
[tabbar addChildViewController:me];
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:tabbar];
nav.navigationBar.barTintColor = [UIColor redColor];
self.window.rootViewController = nav;
UINavigationController+UITabBarController 从ViewController1push到下一个ViewController,UITabBar不会自动消失,可以通过系统自带的hidesBottomBarWhenPushed使UITabBar隐藏,全都用原生的是没有问题的,但是如果结合WKWebView,因为有网络请求和页面加载的时差,导致加载过程中UITabBar会是黑色的背景,用户体验很差:
UITabBarController *tabbar = [[UITabBarController alloc] init];
ViewController1 *v1 = [[ViewController1 alloc] init];
UINavigationController *nav1 = [[UINavigationController alloc] initWithRootViewController:v1];
[tabbar addChildViewController:nav1];
ViewController2 *v2 = [[ViewController2 alloc] init];
UINavigationController *nav2 = [[UINavigationController alloc] initWithRootViewController:v2];
[tabbar addChildViewController:nav2];
self.window.rootViewController = tabbar;
二,合理抽取类
UITabBar显示的ViewController与点击ViewController进去的页面抽取为2重不同的ViewController考虑为以下几点:
①,frame的不同,UITabBar显示的ViewController的frame需要减去tabbar的高度,而点进去的ViewController则不需要减去tabbar的高度
②,点击ViewController进去的页面只需要创建一次,其余交互页面在该页面交互即可,不用每次点击新页面就创建新的ViewController
③,用户体验好,当用户点击了UITabBar页面的ViewController立即创建新的ViewController,显示加载中,接近原生效果
附上实例代码参考:
CommonWebViewVC
- (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler{
NSString *url = [navigationAction.request.URL.absoluteString stringByRemovingPercentEncoding];
NSString* reUrl=[[webView URL] absoluteString];
if ([url containsString:kBaseUrl]||[url containsString:kProductBaseUrl])
{
reUrl=url;
}
if ([self show:reUrl]) {
decisionHandler(WKNavigationActionPolicyCancel);
SecondWebViewController* vc=[[SecondWebViewController alloc] init];
vc.delegate = self;
vc.webViewUrl=reUrl;
NSLog(@"redirectUrl=%@",reUrl);
[self.navigationController pushViewController:vc animated:YES];
}else{
decisionHandler(WKNavigationActionPolicyAllow);
NSLog(@"redirectUrl=%@,allow",reUrl);
}
}
-(BOOL)show:(NSString*) url{
return false;
}
BaseAppWebviewViewController
@interface BaseAppWebviewViewController : CommonWebViewVC
-(NSString*)loadUrl;
@end
- (void)viewDidLoad {
[super viewDidLoad];
self.edgesForExtendedLayout = UIRectEdgeNone;
self.automaticallyAdjustsScrollViewInsets = NO;
self.webView.frame = CGRectMake(0, 0, kScreenWidth, kScreenHeight - kTabbarH);
// Do any additional setup after loading the view.
}
-(BOOL)show:(NSString *)url{//YES 拦截
//如果要跳转的页面不是当前页面和空页面
if (![url isEqualToString:[self loadUrl]]&&![[UrlCollection blankURL] isEqualToString:url]) {
return YES;
}
return NO;
}
- (void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
self.navigationController.navigationBar.hidden = YES;
}
SecondWebViewController
@interface SecondWebViewController : CommonWebViewVC
@end
- (void)viewDidLoad {
[super viewDidLoad];
self.webView.frame = CGRectMake(0, kStatusH, kScreenWidth, kScreenHeight - kStatusH);
}
- (void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
//将status bar 文本颜色设置为白色,当前页面为白色
self.navigationController.navigationBar.barStyle = UIBarStyleDefault;
self.navigationController.navigationBar.hidden = YES;
}