UIViewController+Layout.m 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. //
  2. // UIViewController+Layout.m
  3. // CommonLibrary
  4. //
  5. // Created by Alexi on 3/13/14.
  6. // Copyright (c) 2014 Alexi. All rights reserved.
  7. //
  8. #import "UIViewController+Layout.h"
  9. #import "IOSDeviceConfig.h"
  10. @implementation UIViewController (AsChild)
  11. - (void)setAsChild:(BOOL)asChild
  12. {
  13. }
  14. - (BOOL)asChild
  15. {
  16. return NO;
  17. }
  18. - (void)setChildSize:(CGSize)childSize
  19. {
  20. }
  21. - (CGSize)childSize
  22. {
  23. return CGSizeZero;
  24. }
  25. @end
  26. @implementation UIViewController (Layout)
  27. - (BOOL)shouldAutorotate
  28. {
  29. BOOL isPad = [IOSDeviceConfig sharedConfig].isIPad;
  30. if (isPad)
  31. {
  32. [self layoutSubviewsFrame];
  33. }
  34. return isPad;
  35. }
  36. - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
  37. {
  38. return [self shouldAutorotate];
  39. }
  40. - (UIInterfaceOrientationMask)supportedInterfaceOrientations
  41. {
  42. return UIInterfaceOrientationMaskAll;
  43. }
  44. - (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
  45. {
  46. return UIInterfaceOrientationPortrait;
  47. }
  48. - (void)layoutOnIPhone
  49. {
  50. // iPhone上的布局
  51. }
  52. - (void)layoutOnIPadInPortrait
  53. {
  54. // iPad竖屏布局
  55. [self layoutOnIPhone];
  56. }
  57. - (void)layoutOnIPadInLandScape
  58. {
  59. // iPad横屏布局
  60. [self layoutOnIPhone];
  61. }
  62. - (void)layoutSubviewsFrame
  63. {
  64. if (CGRectIsEmpty(self.view.bounds))
  65. {
  66. return;
  67. }
  68. // App 根据需求决定如何进行布局
  69. IOSDeviceConfig *app = [IOSDeviceConfig sharedConfig];
  70. if (app.isIPad)
  71. {
  72. if (app.isPortrait)
  73. {
  74. [self layoutOnIPadInPortrait];
  75. }
  76. else
  77. {
  78. [self layoutOnIPadInLandScape];
  79. }
  80. }
  81. else
  82. {
  83. [self layoutOnIPhone];
  84. }
  85. }
  86. - (void)layoutOnViewWillAppear
  87. {
  88. [self layoutSubviewsFrame];
  89. }
  90. - (void)addOwnViews
  91. {
  92. // 此处添加界面中的控件
  93. }
  94. - (void)configOwnViews
  95. {
  96. // 此处配置界面中的控件的值
  97. }
  98. @end
  99. @implementation UITabBarController (Layout)
  100. - (BOOL)shouldAutorotate
  101. {
  102. return [self.selectedViewController shouldAutorotate];
  103. }
  104. @end