XG_RootViewController.m 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. //
  2. // XG_RootViewController.m
  3. // MyApp
  4. //
  5. // Created by huxinguang on 2018/9/11.
  6. // Copyright © 2018年 huxinguang. All rights reserved.
  7. //
  8. #import "XG_RootViewController.h"
  9. #import "XG_PickerMacro.h"
  10. @interface XG_RootViewController ()
  11. @end
  12. @implementation XG_RootViewController
  13. -(void)viewWillAppear:(BOOL)animated{
  14. [super viewWillAppear:animated];
  15. self.navigationController.navigationBar.hidden = NO;
  16. }
  17. - (void)viewDidLoad {
  18. [super viewDidLoad];
  19. self.automaticallyAdjustsScrollViewInsets = NO;
  20. self.view.backgroundColor = [UIColor whiteColor];
  21. [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleDefault;
  22. // [self configNavigationBarBackgroundColor:[UIColor whiteColor]];//加上这行可设置导航栏背景色,去掉黑线
  23. [self configWindowLevel];
  24. [self configTitleView];
  25. [self configLeftBarButtonItem];
  26. [self configRightBarButtonItem];
  27. }
  28. - (void)configWindowLevel{
  29. dispatch_async(dispatch_get_main_queue(), ^{
  30. UIWindow *window = [UIApplication sharedApplication].keyWindow;
  31. window.windowLevel = UIWindowLevelNormal;
  32. });
  33. }
  34. - (void)configTitleView{
  35. self.titleView = [[XG_TitleView alloc]initWithFrame:CGRectMake(0, 0, kAppNavigationTitleViewMaxWidth, kAppNavigationTitleViewHeight) style:XG_TitleViewStyleNormal];
  36. self.navigationItem.titleView = self.titleView;
  37. }
  38. //若不要返回按钮或者想替换成其他按钮可重写此方法
  39. - (void)configLeftBarButtonItem{
  40. XG_BarButtonConfiguration *config = [[XG_BarButtonConfiguration alloc]init];
  41. config.type = XG_BarButtonTypeBack;
  42. config.normalImageName = @"navi_back";
  43. self.leftBarButton = [[XG_BarButton alloc]initWithConfiguration:config];
  44. [self.leftBarButton addTarget:self action:@selector(onLeftBarButtonClick) forControlEvents:UIControlEventTouchUpInside];
  45. self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:self.leftBarButton];
  46. }
  47. - (void)configRightBarButtonItem{
  48. }
  49. - (void)onLeftBarButtonClick{
  50. [self.navigationController popViewControllerAnimated:YES];
  51. }
  52. #pragma mark - 修改导航栏背景色
  53. -(void)configNavigationBarBackgroundColor:(UIColor *)barBackgroundColor{
  54. if ([self.navigationController.navigationBar respondsToSelector:@selector(setBackgroundImage:forBarMetrics:)]){
  55. NSArray *subviews =self.navigationController.navigationBar.subviews;
  56. for (id viewObj in subviews) {
  57. if ([UIDevice currentDevice].systemVersion.doubleValue >= 10) {
  58. //iOS10,改变了状态栏的类为_UIBarBackground
  59. NSString *classStr = [NSString stringWithUTF8String:object_getClassName(viewObj)];
  60. if ([classStr isEqualToString:@"_UIBarBackground"]) {
  61. UIImageView *imageView=(UIImageView *)viewObj;
  62. imageView.hidden=YES;
  63. }
  64. }else{
  65. //iOS9以及iOS9之前使用的是_UINavigationBarBackground
  66. NSString *classStr = [NSString stringWithUTF8String:object_getClassName(viewObj)];
  67. if ([classStr isEqualToString:@"_UINavigationBarBackground"]) {
  68. UIImageView *imageView=(UIImageView *)viewObj;
  69. imageView.hidden=YES;
  70. }
  71. }
  72. }
  73. UIImageView *imageView = [self.navigationController.navigationBar viewWithTag:kNavigationBarImageViewTag];
  74. if (!imageView) {
  75. imageView=[[UIImageView alloc] initWithFrame:CGRectMake(0, -20, self.view.frame.size.width, 64)];
  76. imageView.tag = kNavigationBarImageViewTag;
  77. [imageView setBackgroundColor:barBackgroundColor];
  78. dispatch_async(dispatch_get_main_queue(), ^{
  79. [self.navigationController.navigationBar insertSubview:imageView atIndex:0];
  80. });
  81. }else{
  82. [imageView setBackgroundColor:barBackgroundColor];
  83. dispatch_async(dispatch_get_main_queue(), ^{
  84. [self.navigationController.navigationBar sendSubviewToBack:imageView];
  85. });
  86. }
  87. }
  88. }
  89. - (void)didReceiveMemoryWarning {
  90. [super didReceiveMemoryWarning];
  91. // Dispose of any resources that can be recreated.
  92. }
  93. /*
  94. #pragma mark - Navigation
  95. // In a storyboard-based application, you will often want to do a little preparation before navigation
  96. - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
  97. // Get the new view controller using [segue destinationViewController].
  98. // Pass the selected object to the new view controller.
  99. }
  100. */
  101. @end