RDSBaseViewController.m 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518
  1. //
  2. // RDSBaseViewController.m
  3. // SmartLightForBigFish
  4. //
  5. // Created by coderYK on 2017/8/7.
  6. // Copyright © 2017年 RD-iOS. All rights reserved.
  7. //
  8. #import "RDSBaseViewController.h"
  9. #import "NSString+verify.h"
  10. #import "NSString+hanzi.h"
  11. #import "RDSNavPushHelper.h"
  12. #import "UIImage+Extension.h"
  13. @interface RDSBaseViewController ()
  14. @property (nonatomic, copy) void(^contactGetResultBlock)(NSString *result); // ** 联系方式获取的回调*/
  15. @property (nonatomic, strong) UIBarButtonItem *space;
  16. @property (nonatomic, copy) NSString *orgMsg;
  17. @property (nonatomic, assign) BOOL isCheckingOnline;// 检查VCC是否在线
  18. @property (nonatomic, assign) UInt16 checkingAddr;
  19. @end
  20. @implementation RDSBaseViewController
  21. - (void)viewDidLoad {
  22. [super viewDidLoad];
  23. self.view.backgroundColor = RDSViewBgColor;
  24. self.edgesForExtendedLayout = UIRectEdgeNone;
  25. self.navigationController.navigationBar.frame = CGRectMake(0,kSTATUSBAR_HEIGHT, self.navigationController.navigationBar.frame.size.width,44);
  26. }
  27. - (void)viewWillDisappear:(BOOL)animated{
  28. [super viewWillDisappear:animated];
  29. }
  30. - (BOOL)isActive{
  31. // __block BOOL result = NO;
  32. // dispatch_sync(dispatch_get_main_queue(), ^{
  33. // dispatch_semaphore_t sema = dispatch_semaphore_create(0);// 主线程中异步转同步 防止卡死
  34. // // 需要在主线程执行的代码
  35. // result = (self.isViewLoaded && self.view.window);
  36. // dispatch_semaphore_signal(sema);
  37. //
  38. // dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);
  39. // });
  40. return YES;
  41. }
  42. #pragma mark - Nav
  43. - (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated{
  44. if (self.navigationController != nil) {
  45. [self.navigationController pushViewController:viewController animated:animated];
  46. }
  47. else{
  48. [self.tabBarController.navigationController pushViewController:viewController animated:animated];
  49. }
  50. }
  51. - (UIViewController *)popViewControllerAnimated:(BOOL)animated{
  52. if (self.navigationController != nil) {
  53. return [self.navigationController popViewControllerAnimated:animated];
  54. }
  55. else{
  56. return [self.tabBarController.navigationController popViewControllerAnimated:animated];
  57. }
  58. }
  59. - (NSArray *)popToViewController:(UIViewController *)viewController animated:(BOOL)animated{
  60. if (self.navigationController != nil) {
  61. return [self.navigationController popToViewController:viewController animated:animated];
  62. }
  63. else{
  64. return [self.tabBarController.navigationController popToViewController:viewController animated:animated];
  65. }
  66. }
  67. - (void)popToRootViewControllerAnimated:(BOOL)animated{
  68. if (self.navigationController != nil) {
  69. [self.navigationController popToRootViewControllerAnimated:animated];
  70. }
  71. else{
  72. [self.tabBarController.navigationController popToRootViewControllerAnimated:animated];
  73. }
  74. }
  75. - (void)rds_centerPushViewController:(UIViewController *)viewController {
  76. [RDSNavPushHelper sharedInstance].viewControllerToPush = viewController;
  77. // [self.sideMenuViewController hideMenuViewController];
  78. }
  79. #pragma mark -
  80. - (void)updateNavigationBarColor:(UIColor *)color {
  81. // 重置背景和阴影颜色
  82. //navigation标题文字颜色
  83. NSDictionary *dic = @{NSForegroundColorAttributeName : [UIColor whiteColor],
  84. NSFontAttributeName : [UIFont systemFontOfSize:22]};
  85. if (@available(iOS 15.0, *)) {
  86. UINavigationBarAppearance *barApp = [UINavigationBarAppearance new];
  87. // [barApp configureWithOpaqueBackground];
  88. barApp.backgroundColor = color;
  89. barApp.shadowColor = UIColor.clearColor;
  90. barApp.titleTextAttributes = dic;
  91. self.navigationController.navigationBar.scrollEdgeAppearance = barApp;
  92. self.navigationController.navigationBar.standardAppearance = barApp;
  93. }else{
  94. //背景色
  95. self.navigationController.navigationBar.barTintColor = color;
  96. self.navigationController.navigationBar.titleTextAttributes = dic;
  97. [self.navigationController.navigationBar setShadowImage:[UIImage new]];
  98. [self.navigationController.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
  99. }
  100. //不透明
  101. self.navigationController.navigationBar.translucent = NO;
  102. //navigation控件颜色
  103. self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
  104. }
  105. - (void)clearColorNavBar{
  106. //navigation标题文字颜色
  107. NSDictionary *dic = @{NSForegroundColorAttributeName:[UIColor whiteColor],
  108. NSFontAttributeName:[UIFont systemFontOfSize:24]};
  109. if (@available(iOS 15.0, *)) {
  110. UINavigationBarAppearance *barApp = [UINavigationBarAppearance new];
  111. barApp.backgroundColor = [UIColor clearColor];
  112. barApp.titleTextAttributes = dic;
  113. barApp.backgroundEffect = nil;
  114. barApp.shadowColor = nil;
  115. self.navigationController.navigationBar.scrollEdgeAppearance = nil;
  116. self.navigationController.navigationBar.standardAppearance = barApp;
  117. }else{
  118. self.navigationController.navigationBar.titleTextAttributes = dic;
  119. [self.navigationController.navigationBar setShadowImage:[UIImage new]];
  120. [self.navigationController.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
  121. }
  122. //透明
  123. self.navigationController.navigationBar.translucent = YES;
  124. //navigation控件颜色
  125. self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
  126. }
  127. // 圆角边框
  128. - (void)rds_setupArcBorderView:(UIView *)view {
  129. [self rds_setupArcBorderView:view withCornerRadius:5];
  130. }
  131. - (void)rds_setupArcBorderView:(UIView *)view withCornerRadius:(CGFloat)radius {
  132. view.layer.cornerRadius = radius;
  133. view.layer.masksToBounds = YES;
  134. view.layer.borderWidth = 0.6;
  135. view.layer.borderColor = [UIColor lightGrayColor].CGColor;
  136. }
  137. - (void)rds_setupArcView:(UIView *)view {
  138. view.layer.cornerRadius = 5;
  139. view.layer.masksToBounds = YES;
  140. }
  141. - (BOOL)getPhoneNo:(NSString *)string {
  142. BOOL isPhoneNo = NO;
  143. if ([string hasPrefix:@"+"]) {
  144. string = [string substringFromIndex:3];
  145. }
  146. string = [string stringByReplacingOccurrencesOfString:@"-" withString:@""];
  147. if ([string isPhoneNum]) {
  148. isPhoneNo = YES;
  149. BLOCK_SAFE_RUN(_contactGetResultBlock, string)
  150. } else {
  151. [RDSHudShower showErrorWithStatus:@"手机号码格式不正确"];
  152. isPhoneNo = NO;
  153. BLOCK_SAFE_RUN(_contactGetResultBlock, nil)
  154. }
  155. return isPhoneNo;
  156. }
  157. - (UIButton *)rds_addSaveBtnOnNavBarWithAction:(SEL)action {
  158. UIButton *saveBtn = [UIButton buttonWithType:UIButtonTypeCustom];
  159. [saveBtn setTitle:@"保存" forState:UIControlStateNormal];
  160. [saveBtn setTitleColor:RDSThemeColor forState:UIControlStateNormal];
  161. saveBtn.titleLabel.font = [UIFont boldSystemFontOfSize:18];
  162. saveBtn.frame = CGRectMake(0, 0, 44, 30);
  163. [saveBtn addTarget:self action:action forControlEvents:UIControlEventTouchUpInside];
  164. [self.navigationItem setRightBarButtonItem:[[UIBarButtonItem alloc] initWithCustomView:saveBtn]];
  165. return saveBtn;
  166. }
  167. - (void)rds_addAddBtnOnNavBarWithAction:(SEL)action {
  168. UIButton *addBtn = [UIButton buttonWithType:UIButtonTypeCustom];
  169. //addBtn.tintColor = [UIColor darkTextColor];
  170. [addBtn setImage:[[UIImage imageNamed:@"icon_add"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];
  171. addBtn.frame = CGRectMake(0, 0, 44, 30);
  172. //addBtn.layer.cornerRadius = 5;
  173. //addBtn.layer.borderWidth = 1;
  174. //addBtn.layer.borderColor = [UIColor grayColor].CGColor;
  175. [addBtn addTarget:self action:action forControlEvents:UIControlEventTouchUpInside];
  176. [self.navigationItem setRightBarButtonItem:[[UIBarButtonItem alloc] initWithCustomView:addBtn]];
  177. }
  178. - (UIBarButtonItem *)rds_barButtonItemWithImage:(NSString *)imageName andAction:(SEL)action {
  179. UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
  180. btn.tintColor = [UIColor whiteColor];//RDS_DARK_TEXT_COLOR;
  181. UIImage *btnImg = [[UIImage imageNamed:imageName] imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
  182. [btn setImage:btnImg forState:UIControlStateNormal];
  183. [btn setImage:btnImg forState:UIControlStateHighlighted];
  184. [btn sizeToFit];
  185. [btn addTarget:self action:action forControlEvents:UIControlEventTouchUpInside];
  186. UIBarButtonItem *barItem = [[UIBarButtonItem alloc] initWithCustomView:btn];
  187. return barItem;
  188. }
  189. - (void)rds_alertMessage:(NSString *)message {
  190. UIAlertController *ac = [UIAlertController alertControllerWithTitle:message
  191. message:@""
  192. preferredStyle:UIAlertControllerStyleAlert];
  193. UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) {
  194. }];
  195. [ac addAction:okAction];
  196. [self presentViewController:ac animated:YES completion:nil];
  197. }
  198. - (void)rds_alertMessage:(NSString *)message doneAction:(void(^)(void))doneActionBlock {
  199. UIAlertController *ac = [UIAlertController alertControllerWithTitle:@"提示"
  200. message:message
  201. preferredStyle:UIAlertControllerStyleAlert];
  202. UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) {
  203. if (doneActionBlock) {
  204. doneActionBlock();
  205. }
  206. }];
  207. UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * action) {
  208. }];
  209. [ac addAction:okAction]; // A
  210. [ac addAction:cancelAction]; // B
  211. [self presentViewController:ac animated:YES completion:nil];
  212. }
  213. - (void)rds_alertInputTitle:(NSString *)title msg:(NSString *)msg placeholder:(NSString *)placeholder doneAction:(void(^)(NSString *name))doneActionBlock{
  214. UIAlertController *ac = [UIAlertController alertControllerWithTitle:title
  215. message:msg
  216. preferredStyle:UIAlertControllerStyleAlert];
  217. _orgMsg = msg;
  218. // 添加文本框
  219. [ac addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
  220. textField.placeholder = placeholder;
  221. //textField.text = placeholder;
  222. [textField addTarget:self action:@selector(p_alertInputDidChange:) forControlEvents:UIControlEventEditingChanged];
  223. //textField.keyboardType = UIKeyboardTypeASCIICapable;
  224. // if ([title isEqualToString:@"身高"] || [title isEqualToString:@"体重"]) {
  225. // textField.keyboardType = UIKeyboardTypeNumberPad;
  226. // }
  227. }];
  228. UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) {
  229. }];
  230. UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) {
  231. NSString *string = ac.textFields.firstObject.text;
  232. if (doneActionBlock) {
  233. doneActionBlock(string);
  234. }
  235. }];
  236. okAction.enabled = YES;
  237. [ac addAction:cancelAction];
  238. [ac addAction:okAction];
  239. // 修改按钮字体颜色
  240. [cancelAction setValue:RDSBlackColor forKey:@"_titleTextColor"];
  241. // [okAction setValue:RDSThemeColor forKey:@"_titleTextColor"];
  242. // 修改标题字体颜色
  243. if(!kNULLString(title)){
  244. NSMutableAttributedString *titleAtt = [[NSMutableAttributedString alloc] initWithString:title];
  245. [titleAtt addAttribute:NSForegroundColorAttributeName value:kDarkTextColor range:NSMakeRange(0, title.length)];
  246. [titleAtt addAttribute:NSFontAttributeName value:[UIFont boldSystemFontOfSize:18] range:NSMakeRange(0, title.length)];
  247. [ac setValue:titleAtt forKey:@"attributedTitle"];
  248. }
  249. // 修改msg字体颜色
  250. if (!kNULLString(msg)) {
  251. NSMutableAttributedString *msgAtt = [[NSMutableAttributedString alloc] initWithString:msg];
  252. [msgAtt addAttribute:NSForegroundColorAttributeName value:kDarkTextColor range:NSMakeRange(0, msg.length)];
  253. [msgAtt addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:16] range:NSMakeRange(0, msg.length)];
  254. [ac setValue:msgAtt forKey:@"attributedMessage"];
  255. }
  256. // 3.显示控制器
  257. [self presentViewController:ac animated:YES completion:nil];
  258. }
  259. - (void)p_alertInputDidChange:(UITextField *)sender{
  260. UIAlertController *ac = (UIAlertController *)self.presentedViewController;
  261. if (ac) {
  262. UIAlertAction *okAction = ac.actions.lastObject;
  263. NSString *string = ac.textFields.firstObject.text;
  264. NSString *msg = _orgMsg;
  265. NSUInteger length = [string rds_countStringByteLength];
  266. if ([ac.title isEqualToString:@"身高"]) {
  267. NSString *str = string;
  268. if (str.intValue < 20) {
  269. okAction.enabled = NO;
  270. ac.message = @"";
  271. }else if (str.intValue > 300){
  272. okAction.enabled = NO;
  273. ac.message = @"";
  274. }
  275. else{
  276. okAction.enabled = YES;
  277. ac.message = @"";
  278. }
  279. return;
  280. }
  281. else if ([ac.title isEqualToString:@"体重"]){
  282. NSString *str = string;
  283. if (str.intValue < 2) {
  284. okAction.enabled = NO;
  285. ac.message = @"";
  286. }else if (str.intValue > 300){
  287. okAction.enabled = NO;
  288. ac.message = @"";
  289. }
  290. else{
  291. okAction.enabled = YES;
  292. ac.message = @"";
  293. }
  294. return;
  295. }
  296. if (length >0 && length <= 20) {
  297. okAction.enabled = YES;
  298. ac.message = msg;
  299. }
  300. else if (length == 0){
  301. okAction.enabled = YES;
  302. ac.message = msg;
  303. }
  304. else if (length > 20){
  305. okAction.enabled = YES;
  306. ac.message = _orgMsg;//@"名称字数超过限制(10个中文字符或20个英文字符)";
  307. ac.textFields.firstObject.text = [string substringToIndex:length-1];
  308. }
  309. }
  310. }
  311. - (void)rds_alertTimePickerTitle:(NSString *)title date:(NSDate *)date doneAction:(void (^)(NSDate *))doneActionBlock{
  312. UIDatePicker *datePicker = [[UIDatePicker alloc] init];
  313. datePicker.datePickerMode = UIDatePickerModeTime;
  314. if (@available(iOS 13.4, *)) {
  315. datePicker.preferredDatePickerStyle = UIDatePickerStyleWheels;
  316. } else {
  317. // Fallback on earlier versions
  318. }
  319. // 设置当前显示时间
  320. [datePicker setDate:date animated:YES];
  321. UIAlertController *ac = [UIAlertController alertControllerWithTitle:@"\n\n\n\n\n\n\n\n"
  322. message:nil
  323. preferredStyle:UIAlertControllerStyleAlert];
  324. [ac.view addSubview:datePicker];
  325. // 居中
  326. [datePicker mas_makeConstraints:^(MASConstraintMaker *make) {
  327. make.centerX.equalTo(ac.view);
  328. make.centerY.equalTo(ac.view).with.offset(-22);
  329. }];
  330. UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"DONE" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) {
  331. NSDateFormatter* dateFormat = [[NSDateFormatter alloc] init];
  332. [dateFormat setDateFormat:@"HH:mm"];
  333. NSString *dateString = [dateFormat stringFromDate:datePicker.date];
  334. NSLog(@"%@",dateString);
  335. if (doneActionBlock) {
  336. doneActionBlock(datePicker.date);
  337. }
  338. }];
  339. UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"CANCEL" style:UIAlertActionStyleCancel handler:^(UIAlertAction * action) {
  340. }];
  341. [ac addAction:okAction];
  342. [ac addAction:cancelAction];
  343. [self presentViewController:ac animated:YES completion:nil];
  344. }
  345. - (void)rds_showControllerOnWindow {
  346. // RDSSideMenuViewController *rootVC = (RDSSideMenuViewController *)[UIApplication sharedApplication].keyWindow.rootViewController;
  347. // [rootVC.contentViewController addChildViewController:self];
  348. [[UIApplication sharedApplication].keyWindow addSubview:self.view];
  349. }
  350. - (void)rds_dismissControllerOnWindow {
  351. [self.view removeFromSuperview];
  352. [self removeFromParentViewController];
  353. }
  354. - (NSString *)rds_getHexNameFromString:(NSString *)name withByte:(NSUInteger)byte {
  355. NSString *hexName = [self rds_stringTohexString:name];
  356. int offset = (int)(byte*2 - hexName.length);
  357. for (int i = 0; i < offset; i++) {
  358. hexName = [NSString stringWithFormat:@"%@0",hexName];
  359. }
  360. return hexName;
  361. }
  362. - (NSString *)rds_stringTohexString:(NSString *)string {
  363. NSMutableString *strM = [NSMutableString string];
  364. for (int i=0; i<string.length; i++) {
  365. NSString *code = [NSString stringWithFormat:@"%02x", [string characterAtIndex:i]];
  366. [strM appendString:code];
  367. }
  368. return strM;
  369. }
  370. - (void)rds_showJuHuaOnWindow {
  371. if (self.juHua == nil) {
  372. UIActivityIndicatorView *juHua = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
  373. self.juHua = juHua;
  374. self.juHua.hidesWhenStopped = YES;
  375. [[UIApplication sharedApplication].keyWindow addSubview:self.juHua];
  376. self.juHua.center = [UIApplication sharedApplication].keyWindow.center;
  377. }
  378. [self.juHua startAnimating];
  379. }
  380. - (NSArray<UIBarButtonItem *> *)rds_barItemsWithImage:(UIImage *)image target:(id)target andSelector:(SEL)selector needAdjustSpace:(BOOL)need {
  381. UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 35, 35)];
  382. // UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
  383. // [button setBackgroundImage:image forState:UIControlStateNormal];
  384. // [button sizeToFit];
  385. [button addTarget:target action:selector forControlEvents:UIControlEventTouchUpInside];
  386. UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithCustomView:button];
  387. if (need) {
  388. return @[self.space, item];
  389. }
  390. return @[item];
  391. }
  392. - (UIBarButtonItem *)space {
  393. if (_space == nil) {
  394. _space = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace
  395. target:nil action:nil];
  396. _space.width = isIOS11? -10 : -5;
  397. }
  398. return _space;
  399. }
  400. - (void)didReceiveMemoryWarning {
  401. [super didReceiveMemoryWarning];
  402. // Dispose of any resources that can be recreated.
  403. }
  404. @end