RootViewController.m 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. //
  2. // RootViewController.m
  3. // XenonSDK
  4. //
  5. // Created by SAGESSE on 2019/5/29.
  6. // Copyright © 2019 SAGESSE. All rights reserved.
  7. //
  8. #import "XenonSDK.h"
  9. #import "XSUtils.h"
  10. #import "RootViewController.h"
  11. #import "NavigationAnimator.h"
  12. #import "HelperViewController.h"
  13. #import "UserLoginPhoneViewController.h"
  14. #import "UserLoginAccountViewController.h"
  15. #import "UserRegisterAccountViewController.h"
  16. #import "UserProtocolViewController.h"
  17. #import "UserRestPasswordViewController.h"
  18. #import "UserRestPassword2ViewController.h"
  19. #import "UserPrivacyProlicyViewController.h"
  20. #import "UserForgetPasswordViewController.h"
  21. @interface RootViewController () <UINavigationControllerDelegate>
  22. @property (nonatomic, strong) id keybardInfo;
  23. @end
  24. @implementation RootViewController
  25. - (instancetype)initWithRootViewController:(UIViewController*)rootViewController {
  26. self = [self initWithNibName:nil bundle:XenonSDK.sharedSDK.bundle];
  27. self.embedViewController = [[UINavigationController alloc] initWithRootViewController:rootViewController];
  28. self.modalPresentationStyle = UIModalPresentationOverFullScreen;
  29. self.embedViewController.delegate = self;
  30. return self;
  31. }
  32. - (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
  33. return [super initWithNibName:@"RootViewController" bundle:nibBundleOrNil];
  34. }
  35. - (void)viewDidLoad {
  36. [super viewDidLoad];
  37. // Do any additional setup after loading the view.
  38. //
  39. self.view.backgroundColor = UIColor.clearColor;
  40. [self.view addSubview:self.embedView];
  41. [self viewWillLayoutSubviews];
  42. // Update font size dynamically.
  43. if (self.titleLabel != nil) {
  44. self.titleLabel.font = [self.titleLabel.font fontWithSize:CGFloatBasedI414(26)];
  45. // foce load view.
  46. (void)self.embedViewController.topViewController.view;
  47. // self.titleLabel.text = (self.embedViewController.topViewController as? BaseViewController)?.topTitle
  48. }
  49. if (self.theme == nil) {
  50. UIToolbar* toolbar = [[UIToolbar alloc] initWithFrame:self.view.bounds];
  51. toolbar.barStyle = UIBarStyleDefault;
  52. toolbar.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
  53. toolbar.userInteractionEnabled = false;
  54. //toolbar.setShadowImage(UIImage(), forToolbarPosition: .any)
  55. [self.embedView insertSubview:toolbar atIndex:0];
  56. }
  57. //self.embedController.isNavigationBarHidden = true
  58. self.embedViewController.view.frame = self.embedView.bounds;
  59. self.embedViewController.view.backgroundColor = UIColor.clearColor;
  60. self.embedViewController.view.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
  61. // self.embedViewController.navigationBar.isTranslucent = true
  62. // self.embedViewController.navigationBar.setBackgroundImage(.init(), for: .default)
  63. // self.embedViewController.navigationBar.shadowImage = .init()
  64. self.embedViewController.navigationBarHidden = true;
  65. [self addChildViewController:self.embedViewController];
  66. [self.embedView addSubview:self.embedViewController.view];
  67. }
  68. - (void)viewWillAppear:(BOOL)animated {
  69. [super viewWillAppear:animated];
  70. [NSNotificationCenter.defaultCenter addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
  71. [NSNotificationCenter.defaultCenter addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
  72. [NSNotificationCenter.defaultCenter addObserver:self selector:@selector(textFieldWillEditing:) name:UITextFieldTextDidBeginEditingNotification object:nil];
  73. }
  74. - (void)viewDidDisappear:(BOOL)animated {
  75. [super viewDidDisappear:animated];
  76. [NSNotificationCenter.defaultCenter removeObserver:self];
  77. }
  78. - (id<UIViewControllerAnimatedTransitioning>)navigationController:(UINavigationController *)navigationController animationControllerForOperation:(UINavigationControllerOperation)operation fromViewController:(UIViewController *)fromVC toViewController:(UIViewController *)toVC {
  79. return [[NavigationAnimator alloc] initWithOperation:operation titleView:_titleLabel];
  80. }
  81. // MARK: -
  82. - (void)textFieldWillEditing:(id)sender {
  83. if (self.keybardInfo != nil) {
  84. [self keyboardWillShow:self.keybardInfo];
  85. }
  86. }
  87. // Get current firset responder in ui hierarchy.
  88. - (id)firstResponderInResponder:(id)responder {
  89. if ([responder isFirstResponder]) {
  90. return responder;
  91. }
  92. if ([responder isKindOfClass:UIViewController.class]) {
  93. return [self firstResponderInResponder:[responder view]];
  94. }
  95. if ([responder isKindOfClass:UIView.class]) {
  96. for (UIView* view in [responder subviews]) {
  97. id r = [self firstResponderInResponder:view];
  98. if (r != nil) {
  99. return r;
  100. }
  101. }
  102. }
  103. return nil;
  104. }
  105. - (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
  106. [super touchesEnded:touches withEvent:event];
  107. // Resign all first responder.
  108. [self.view endEditing:YES];
  109. }
  110. /// When show keyboard add view animation.
  111. - (void)keyboardWillShow:(NSNotification*)sender {
  112. // Gets the UITextField in first responder.
  113. NSDictionary* userInfo = sender.userInfo;
  114. UITextField* firstResponder = (id)[self firstResponderInResponder:self.view];
  115. if (userInfo == nil || firstResponder == nil) {
  116. return;
  117. }
  118. CGRect textField = [firstResponder convertRect:firstResponder.bounds toView:self.view];
  119. // Get animation duration.
  120. NSTimeInterval duration = [userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue];
  121. CGRect frame = [userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
  122. CGFloat offset = fmin((CGRectGetMinY(frame) - textField.size.height) / 2 - CGRectGetMinY(textField), 0);
  123. // Make animation.
  124. [UIView animateWithDuration:duration animations:^{
  125. UIViewAnimationCurve type = [userInfo[UIKeyboardAnimationCurveUserInfoKey] integerValue];;
  126. [UIView setAnimationCurve:type];
  127. self.view.transform = CGAffineTransformMakeTranslation(0, offset);
  128. }];
  129. // Save keyboard info.
  130. self.keybardInfo = sender;
  131. }
  132. /// When hide keyboard add view animation.
  133. - (void)keyboardWillHide:(NSNotification*)sender {
  134. // Get animation duration.
  135. NSDictionary* userInfo = sender.userInfo;
  136. NSTimeInterval duration = [userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue];
  137. [UIView animateWithDuration:duration animations:^{
  138. UIViewAnimationCurve type = [userInfo[UIKeyboardAnimationCurveUserInfoKey] integerValue];;
  139. [UIView setAnimationCurve:type];
  140. self.view.transform = CGAffineTransformIdentity;
  141. }];
  142. // Clean keybard info.
  143. self.keybardInfo = nil;
  144. }
  145. @end
  146. @implementation UIViewController (XenonSDK)
  147. - (void)sdk_dismissViewController:(id)sender {
  148. if (self.navigationController.viewControllers.count <= 1) {
  149. [self dismissViewControllerAnimated:YES completion:nil];
  150. return;
  151. }
  152. [self.navigationController popViewControllerAnimated:YES];
  153. }
  154. - (void)sdk_showViewControllerForPhoneLogin:(id)sender {
  155. [self.view endEditing:YES];
  156. [self.navigationController pushViewController:UserLoginPhoneViewController.new animated:YES];
  157. }
  158. // MARK: -
  159. #pragma mark - =======forgetPassword Action======
  160. - (void)sdk_showViewControllerForForgetPasswod:(id)sender {
  161. [self.view endEditing:YES];
  162. [self.navigationController pushViewController:UserForgetPasswordViewController.new animated:YES];
  163. }
  164. - (void)sdk_showViewControllerForResetPasswod:(id)sender {
  165. [self.view endEditing:YES];
  166. [self.navigationController pushViewController:UserRestPassword2ViewController.new animated:YES];
  167. }
  168. ///用户隐私协议
  169. - (void)sdk_showViewControllerForUserPrivacy:(id)sender {
  170. [self.view endEditing:YES];
  171. UserPrivacyProlicyViewController *prolicyVc = [[UserPrivacyProlicyViewController alloc]init];
  172. [self presentViewController:prolicyVc animated:YES completion:nil];
  173. }
  174. //用户注册协议
  175. - (void)sdk_showViewControllerForUserProtocol:(id)sender {
  176. [self.view endEditing:YES];
  177. [self.navigationController pushViewController:UserProtocolViewController.new animated:YES];
  178. }
  179. - (void)sdk_showViewControllerForCustomerService:(id)sender {
  180. [self.view endEditing:YES];
  181. [self.navigationController pushViewController:HelperViewController.new animated:YES];
  182. }
  183. @end
  184. @implementation UITextField (XenonSDK)
  185. - (void)setPlaceholderColor:(UIColor*)color {
  186. if (color == nil) {
  187. self.placeholder = self.placeholder;
  188. return;
  189. }
  190. if (self.placeholder == nil) {
  191. return;
  192. }
  193. id mas = [[NSMutableAttributedString alloc] initWithString:self.placeholder attributes:@{NSForegroundColorAttributeName: color}];
  194. self.attributedPlaceholder = mas;
  195. }
  196. - (UIColor*)placeholderColor {
  197. return nil;
  198. }
  199. @end
  200. @implementation CALayer (XenonSDK)
  201. - (void)setCornerRadiusBasedI375:(CGFloat)radius {
  202. self.cornerRadius = CGFloatBasedI375(radius);
  203. }
  204. - (CGFloat)cornerRadiusBasedI375 {
  205. return self.cornerRadius;
  206. }
  207. @end