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];
代码语言:javascript复制自定义返回按钮后,系统的侧滑返回会失效,之前我一直每个界面都设置,打开关闭,后来发现直接可以设置所有的
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;
}
}
代码语言:javascript复制这个是用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);
}
代码语言:javascript复制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;
}
}
}
代码语言:javascript复制请求是参数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];
}```