[iOS]开发中遇见的一些坑

版权所有,禁止匿名转载;禁止商业使用。

这篇文章是本人在项目开发中曾经遇见的坑,记录下来分享给大家,希望能给需要的童鞋一些帮助,后续也将不断更新!

  • 开启系统自带侧滑返回

    如果使用的是navigationbar做的导航栏,那么修改导航栏后,你就会发现自带的侧滑返回不能使用了,那么在nav的根控制器里面加上这句,就OK了

// Objective - C
self.navigationController.interactivePopGestureRecognizer.delegate = (id<UIGestureRecognizerDelegate>)self;
// swift因为是强类型,所以不能像上面那么写
// 你只需要根视图遵循UIGestureRecognizerDelegate协议,然后加上下面这句就OK了
navigationController.interactivePopGestureRecognizer.delegate = self;

此处楼主一般写项目时,从不使用自带的导航栏,在控制器的基类将导航栏隐藏掉,然后新建一个View,作为导航栏,这样的话,就不用考虑手势侧滑返回的问题了.到时在每个控制器直接调用添加导航栏的方法就可以了,代码如下:

-(void)setNaVWithTile:(NSString *)title imageName:(NSString *)image andRightBtn:(NSDictionary *)btnDic
{
    UIView *V = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 64)];
    V.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:V];
    if (image == nil) {
       image = @"back-arrow";
    }
    UIImageView *imv = [[UIImageView alloc]initWithFrame:CGRectMake(20, 30, 12.5, 21)];
    imv.image = [UIImage imageNamed:[NSString stringWithFormat:@"%@",image]];
    [V addSubview:imv];
    UIButton *backBtn = [UIButton buttonWithFrame:CGRectMake(0, 20, 40, 40) title:nil backgroundColor:nil type:UIButtonTypeCustom target:self action:@selector(backBtnAction:)];
    [V addSubview:backBtn]; 
    UILabel *titlelab = [UILabel lableWithFrame:CGRectMake(40, 20, kScreenWidth-80, 44) title:title textFont:16 textColor:UIColorFromRGB(0x333333)];
    titlelab.font = [UIFont systemFontOfSize:17];
    titlelab.textAlignment = NSTextAlignmentCenter;
//    titlelab.backgroundColor = [UIColor redColor];
    [V addSubview:titlelab]; 
    UIView *line = [[UIView alloc]initWithFrame:CGRectMake(0, 63, kScreenWidth, 1)];
    line.backgroundColor = UIColorFromRGB(0xe8e8e8);
    [V addSubview:line]; 
    if (btnDic != nil) {
        UIButton * btn = [[UIButton alloc]init];
        btn.frame = CGRectMake(V.width  - 60, backBtn.top, 60, 40);
        btn.titleLabel.font = [UIFont systemFontOfSize:14];
        if ([[btnDic objectForKey:@"title"] isEqualToString:@""]) {
            [btn setImage:[UIImage imageNamed:[NSString stringWithFormat:@"%@",[btnDic objectForKey:@"image"]]] forState:UIControlStateNormal];
                }else{
            [btn setTitle:[NSString stringWithFormat:@"%@",[btnDic objectForKey:@"title"]] forState:UIControlStateNormal];
            [btn setTitleColor:UIColorFromRGB(0x666666) forState:UIControlStateNormal];
        }
        [btn addTarget:self action:@selector(BtnAction:) forControlEvents:UIControlEventTouchUpInside];
            [V addSubview:btn];
    }
}
-(void)backBtnAction:(UIButton *)sender{
    [self.navigationController popViewControllerAnimated:YES];
}
  • 保持手机常亮

    在项目需求上,在固定页面,我们可能会有保持手机屏幕常亮的状态,如果遇上的话,在当前控制器页面加上下面这句代码就可以了,在离开此页面时,将常亮关闭改为NO就OK!

[[UIApplication sharedApplication] setIdleTimerDisabled:YES];
  • 点击H5页面的按钮,获取H5页面的超链接

    直接贴代码,你一看就懂了,遵循<UIWebViewDelegate>

给贴个示例H5链接:https://shoptest.2or3m.com/Shop/Mincircle/Official
在代理方法里写

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType{

    NSString *requestString = [[[request URL] absoluteString]stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    DDLog(@"requestString : %@",requestString);

    NSArray *components = [requestString componentsSeparatedByString:@"|"];
    DDLog(@"=components=====%@",components);
    
    NSString *str1 = [components objectAtIndex:0];
    DDLog(@"str1:::%@",str1);
    
    NSArray *array2 = [str1 componentsSeparatedByString:@"/"];
    DDLog(@"array2:====%@",array2);
    
    NSInteger coun = array2.count;
    NSString *method = array2[coun-1];
    DDLog(@"method:===%@",method);
    
    if ([method isEqualToString:@"good_id"])//查看详情,其中红色部分是HTML5跟咱们约定好的,相应H5上的按钮的点击事件后,H5发送超链接,客户端一旦收到这个超链接信号。把其中的点击按钮的约定的信号标示符截取出来跟本地的标示符来进行匹配,如果匹配成功,那么就执行相应的操作,详情见如下所示。
    {
       //根据条件在此做相应的操作

        return NO;
    }else if ([method isEqualToString:@"video_id"])
    {
       //根据条件在此作相应的操作

        return NO;
    }
    return YES;
}
0 0