实战荟萃-UI篇(持续更新)

265 阅读1分钟

一. 前言

平时在处理问题的时候,经常会遇到一些奇奇怪怪的问题,今天在这里将其记录下来。这里将会列举几个常用的UI问题进行讲解

二. 导航栏

iOS导航栏绝对是个巨坑。和很多朋友聊天都是自己实现了一套导航栏。当然,我个人是比较推崇用系统的方法。因为好处好几个:1.新特性推出之后简单易改 2.代码安装包体积的问题

1. 导航栏、状态栏的显示与隐藏

一般直接用系统的导航栏状态栏我们会这么做,优点:使用简单方便

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    
    //恢复导航栏和状态栏
    [self.navigationController setNavigationBarHidden:NO animated:YES];
    [[UIApplication sharedApplication] setStatusBarHidden:NO];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    //隐藏导航栏和状态栏
    [self.navigationController setNavigationBarHidden:YES animated:YES];
    [[UIApplication sharedApplication] setStatusBarHidden:YES];
}
- (BOOL)prefersStatusBarHidden
{
    return YES;
}

2. 自定义导航栏

三. 键盘

UITableView上的键盘隐藏与弹起 以前碰到网上的代码实属坑,弹出的时候还有延时。凡事还是得自己动手试一试

-(void)registerNotifications
{
    //注册通知
    CZ_AddObj2DeftNotiCenter(self, @selector(keyboardWillShow:), UIKeyboardWillShowNotification, nil);
    CZ_AddObj2DeftNotiCenter(self, @selector(keyboardWillHide:), UIKeyboardWillHideNotification, nil);
}
#pragma mark- TableView
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [[UIApplication sharedApplication] sendAction:@selector(resignFirstResponder) to:nil from:nil forEvent:nil];
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}
#pragma mark- keyboard
- (void)keyboardWillShow:(NSNotification*)notification
{
    NSDictionary* info = [notification userInfo];
    CGSize kbSize = [[info objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size;
//    NSInteger curve = [notification.userInfo[UIKeyboardAnimationCurveUserInfoKey] integerValue];
//    NSTimeInterval duration = [notification.userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue];
    
    CGRect oldFram = self.cardDetailTableView.frame;
    self.cardDetailTableView.frame = CGRectMake(oldFram.origin.x,- kbSize.height, oldFram.size.width, oldFram.size.height);
    
    
    UIEdgeInsets contentInsets = UIEdgeInsetsMake(kbSize.height, 0.0, 0.0, 0.0);
    self.cardDetailTableView.contentInset = contentInsets;
    self.cardDetailTableView.scrollIndicatorInsets = contentInsets;
    
    if(_tableRecognizer) {
        [_cardDetailTableView removeGestureRecognizer:_tableRecognizer];
    }
    _tableRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hideKeyboard)];
    _tableRecognizer.numberOfTapsRequired = 1;
    [_cardDetailTableView addGestureRecognizer:_tableRecognizer];
}


- (void)keyboardWillHide:(NSNotification *)notification
{
//    NSDictionary* info = [notification userInfo];
    CGRect oldFram = self.cardDetailTableView.frame;
    self.cardDetailTableView.frame = CGRectMake(oldFram.origin.x, 0.0, oldFram.size.width, oldFram.size.height);
    self.cardDetailTableView.contentInset = UIEdgeInsetsZero;
    self.cardDetailTableView.scrollIndicatorInsets = UIEdgeInsetsZero;
}

- (void)hideKeyboard
{
    [[UIApplication sharedApplication] sendAction:@selector(resignFirstResponder) to:nil from:nil forEvent:nil];
    if(_tableRecognizer) {
        _tableRecognizer.delegate = nil;
        [_cardDetailTableView removeGestureRecognizer:_tableRecognizer];
    }
}