iOS开发知识点

今天整理项目,为项目添加注释,发现有些东西需要保存一下,将来好使用。

  • 设置navBar的背景,去掉黑线,试了好长时间,查了好多,这个对我适用
  • 设置navBar上item的颜色,以及nav Title的颜色和字体大小
  • 替换系统返回按钮的图片,设计说自带的太丑,一开始是自定义返回的View,后来发现不用那么麻烦,直接有方法可以替换
// 设置navBar背景,这样设置可去掉那个黑线
[[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:@"bg_bar"] forBarMetrics:UIBarMetricsDefault];
[[UINavigationBar appearance] setShadowImage:[[UIImage alloc] init]];
[[UINavigationBar appearance] setTranslucent:NO];
    
// 设置navBar的按钮的tintColor,及title字体大小和颜色
[[UINavigationBar appearance] setTintColor:[UIColor whiteColor]];
[[UINavigationBar appearance] setTitleTextAttributes:@{NSFontAttributeName: [UIFont fontWithName:[MKCommonData commonNavigationBarFontType] size:20.0], NSForegroundColorAttributeName: [UIColor whiteColor]}];
    
// 设置navBar返回按钮的背景图片, 必须两个方法一起才有用
[[UINavigationBar appearance] setBackIndicatorImage:[UIImage imageNamed:@"icon_white"]];
[[UINavigationBar appearance] setBackIndicatorTransitionMaskImage:[UIImage imageNamed:@"icon_white"]];

// 去除返回按钮的文字
 [[UIBarButtonItem appearance] setBackButtonTitlePositionAdjustment:UIOffsetMake(0, -60) forBarMetrics:UIBarMetricsDefault];

自定义返回按钮后,系统的侧滑返回会失效,之前我一直每个界面都设置,打开关闭,后来发现直接可以设置所有的

self.navigationController.interactivePopGestureRecognizer.delegate = self; // 侧滑返回,自定义返回按钮后生效,在最顶部设置可以在Push出来的界面都有效

#pragma mark - gestureRecognizer delegate - 
// 侧滑返回,如果是首页就不启用,不是首页则启用
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer {
    if (self.navigationController.viewControllers.count == 1) {
        return NO;
    } else {
        return YES;
    }
}

这个是用GCD实现的倒计时,发送验证码的实现

#pragma mark - GCD 实现倒计时
- (void)countDown
{
    __block int timeout = 61; // 倒计时时间
    self.countDownLabel.text = [NSString stringWithFormat:@"接收短信大概需要%d秒钟", timeout];
    self.countDownLabel.textColor = [UIColor lightGrayColor];
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    timer = dispatch_source_create((DISPATCH_SOURCE_TYPE_TIMER), 0, 0, queue);
    dispatch_source_set_timer(timer, dispatch_walltime(NULL, 0), 1.0 * NSEC_PER_SEC, 0 * NSEC_PER_SEC); // 每秒执行
    dispatch_source_set_event_handler(timer, ^{
        timeout--;
        if (timeout <= 0) {
            // 倒计时结束,关闭
            dispatch_source_cancel(timer);
            dispatch_async(dispatch_get_main_queue(), ^{
                // 结束                
                
            });
        } else {
            NSString * strTime = [NSString stringWithFormat:@"接收短信大概需要%d秒钟", timeout];
            dispatch_async(dispatch_get_main_queue(), ^{
                // 更新timeLabel的显示
                self.countDownLabel.text = strTime;
            });
        }
    });
    dispatch_resume(timer);
}

searchBar自带的取消按钮是cancel,英文的,但是产品强迫要中文的,所以就只能改啊

// searchBar开始编辑时改变取消按钮的文字
- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar {
    _searchBarView.searchBar.showsCancelButton = YES;
    NSArray *subViews;
    CGFloat deviceVersion = [UIDevice currentDevice].systemVersion.floatValue;
    if (deviceVersion >= 7.0) {
        subViews = [_searchBarView.searchBar.subviews[0] subviews];
    } else {
        subViews = _searchBarView.searchBar.subviews;
    }
    
    for (id view in subViews) {
        if ([view isKindOfClass:[UIButton class]]) {
            UIButton *cancelButton = (UIButton *)view;
            [cancelButton setTitle:@"取消" forState:UIControlStateNormal];
            break;
        }
    }
}

请求是参数json string格式,之前的话,我一直认为参数是字典类型的,后来仔细看了一下,发现原来是id,可怜我自己封装的都是NSDictionary,我见过三种类型的参数,a. 直接字典;b. 将参数内的那个dic变成json string,然后把这个string当成一个value,赋给一个key;
c. json string。说多了都是泪。

// 将参数转为json string
- (NSString *)convertJSONStringWithObject:(id)jsonObject
{
    NSData *jsonData;
    if ([NSJSONSerialization isValidJSONObject:jsonObject]) {
        NSError *error;
        jsonData = [NSJSONSerialization dataWithJSONObject:jsonObject
                                                   options:NSJSONWritingPrettyPrinted
                                                     error:&error];
        if (error) {
            return nil;
        }
    }
    else {
        jsonData = jsonObject;
    }
    NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
    
    NSMutableString *mutStr = [NSMutableString stringWithString:jsonString];
    NSRange range = {0, jsonString.length};
    [mutStr replaceOccurrencesOfString:@" " withString:@"" options:NSLiteralSearch range:range];
    NSRange range2 = {0, mutStr.length};
    [mutStr replaceOccurrencesOfString:@"\n" withString:@"" options:NSLiteralSearch range:range2];
    return [NSString stringWithFormat:@"%@", mutStr];
}```
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 134,981评论 19 139
  • 网络阶段# 应用程序如何连接互联网## 1.基于HTTP协议 HTTP:超文本传输协议(Hyper-Text Ma...
    睡雨阅读 652评论 0 50
  • 以下都是自己在iOS开发的过程中遇到的问题,自己总结出来的小知识点。 1.UITableViewCell的cont...
    Code_Ninja阅读 4,526评论 3 14
  • 1.请简述视图控制器的生命周期 (1)alloc:创建对象,分配空间 (2)init:初始化对象 (3)loadV...
    4b5317535aa5阅读 252评论 0 0
  • 1. 热,我觉得很热。 我抱着一个很热的包,坐在一个很热的座位上,很热的我和很热的包以及这个很热的座位都在一辆很热...
    月动阅读 1,774评论 10 39