CommonBaseViewController.m 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. //
  2. // CommonBaseViewController.m
  3. // CommonLibrary
  4. //
  5. // Created by Alexi Chen on 2/28/13.
  6. // Copyright (c) 2013 AlexiChen. All rights reserved.
  7. //
  8. #import "CommonBaseViewController.h"
  9. #import "UIViewController+Layout.h"
  10. #import "IOSDeviceConfig.h"
  11. @implementation CommonBaseViewController
  12. - (instancetype)init
  13. {
  14. if (self = [super init])
  15. {
  16. [self configParams];
  17. }
  18. return self;
  19. }
  20. - (void)configParams
  21. {
  22. }
  23. #pragma mark -
  24. #pragma Rotate Methods
  25. //- (void)viewWillAppear:(BOOL)animated
  26. //{
  27. // [super viewWillAppear:animated];
  28. // [self layoutOnViewWillAppear];
  29. //}
  30. - (void)viewDidAppear:(BOOL)animated
  31. {
  32. [super viewDidAppear:animated];
  33. [self layoutOnViewWillAppear];
  34. }
  35. - (BOOL)shouldAutorotate
  36. {
  37. BOOL isPad = [IOSDeviceConfig sharedConfig].isIPad;
  38. if (isPad)
  39. {
  40. [self layoutSubviewsFrame];
  41. }
  42. return isPad;
  43. }
  44. - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
  45. {
  46. return [self shouldAutorotate];
  47. }
  48. //- (NSUInteger)supportedInterfaceOrientations
  49. //{
  50. // return [self.viewControllers.lastObject supportedInterfaceOrientations];
  51. //}
  52. //
  53. //- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
  54. //{
  55. // return [self.viewControllers.lastObject preferredInterfaceOrientationForPresentation];
  56. //}
  57. - (UIInterfaceOrientationMask)supportedInterfaceOrientations
  58. {
  59. return UIInterfaceOrientationMaskAll;
  60. }
  61. - (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
  62. {
  63. return UIInterfaceOrientationPortrait;
  64. }
  65. //- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
  66. //{
  67. // [self layoutSubviewsFrame];
  68. //}
  69. - (BOOL)sameWithIOS6
  70. {
  71. return YES;
  72. }
  73. - (void)configContainer
  74. {
  75. self.edgesForExtendedLayout = UIRectEdgeNone;
  76. self.extendedLayoutIncludesOpaqueBars = NO;
  77. self.navigationController.navigationBar.translucent = NO;
  78. self.tabBarController.tabBar.translucent = NO;
  79. self.navigationController.interactivePopGestureRecognizer.enabled = YES;
  80. }
  81. - (void)viewDidLoad
  82. {
  83. [super viewDidLoad];
  84. self.view.backgroundColor = kBackGroundColor;
  85. [self setUpLocalizationString];
  86. // if ([self sameWithIOS6])
  87. // {
  88. // if ([IOSDeviceConfig sharedConfig].isIOS7)
  89. // {
  90. // self.edgesForExtendedLayout = UIRectEdgeNone;
  91. // self.extendedLayoutIncludesOpaqueBars = NO;
  92. // self.modalPresentationCapturesStatusBarAppearance = NO;
  93. // }
  94. // }
  95. //
  96. [self configContainer];
  97. if ([self isAutoLayout])
  98. {
  99. [self autoLayoutOwnViews];
  100. }
  101. else
  102. {
  103. if ([self hasBackgroundView])
  104. {
  105. [self addBackground];
  106. [self configBackground];
  107. }
  108. // 在此外添加界面的各个控件
  109. [self addOwnViews];
  110. // 在此设置各个控件的值
  111. [self configOwnViews];
  112. // 对自身的控件进行设置区域
  113. [self layoutSubviewsFrame];
  114. // _statusBarOrientation = [UIApplication sharedApplication].statusBarOrientation;
  115. // app.isPortrait = UIInterfaceOrientationIsPortrait(intf);
  116. }
  117. }
  118. - (BOOL)hasBackgroundView
  119. {
  120. return NO;
  121. }
  122. - (void)addBackground
  123. {
  124. _backgroundView = [[UIImageView alloc] init];
  125. [self.view addSubview:_backgroundView];
  126. CommonRelease(_backgroundView);
  127. }
  128. - (void)configBackground
  129. {
  130. IOSDeviceConfig *ios = [IOSDeviceConfig sharedConfig];
  131. if (ios.isIPhone5)
  132. {
  133. // UIImage *bg = UIImageNamed(kAppBgImg);
  134. // _backgroundView.image = bg;
  135. }
  136. else
  137. {
  138. _backgroundView.backgroundColor = [UIColor flatWhiteColor];
  139. }
  140. }
  141. #pragma mark -
  142. #pragma Layout Methods
  143. - (void)layoutBackground
  144. {
  145. _backgroundView.frame = self.view.bounds;
  146. }
  147. - (void)viewWillLayoutSubviews
  148. {
  149. if (![self asChild])
  150. {
  151. [super viewWillLayoutSubviews];
  152. }
  153. else
  154. {
  155. if (CGSizeEqualToSize(self.childSize, CGSizeZero))
  156. {
  157. [super viewWillLayoutSubviews];
  158. }
  159. else
  160. {
  161. CGSize size = [self childSize];
  162. self.view.bounds = CGRectMake(0, 0, size.width, size.height);
  163. }
  164. }
  165. }
  166. - (void)layoutSubviewsFrame
  167. {
  168. if ([self isAutoLayout])
  169. {
  170. return;
  171. }
  172. if ([self hasBackgroundView])
  173. {
  174. [self layoutBackground];
  175. }
  176. [super layoutSubviewsFrame];
  177. }
  178. - (UIStatusBarStyle)preferredStatusBarStyle
  179. {
  180. return UIStatusBarStyleLightContent;
  181. }
  182. @end
  183. @implementation CommonBaseViewController (AutoLayout)
  184. // 是否支持autoLayout
  185. - (BOOL)isAutoLayout
  186. {
  187. return NO;
  188. }
  189. // 添加自动布局相关的constraints
  190. - (void)autoLayoutOwnViews
  191. {
  192. // 添加自动布局相关的内容
  193. }
  194. @end