Angzn's Blog

飘荡空中 行于云里.

Xcode Tips


在使用Xcode的时候,经常会遇到各种“莫名其妙”的‘BUG’。本来写完一段完美的代码,想来测试一下自己的成果,结果,run,Xcode给你各种莫名的xxx信息,这会你要恼羞成怒了。

一、问题

1.Could not inspect the application package.

在 Xcode 6 上出现一次正常,一次弹出 Could not inspect the application package 错误问题,原因可能是:

1. 项目 **Product Name** 包含非拉丁文(即:包含中文),所以只需要把你的 Product Name 修改一下就 OK 可以;
2. 项目包含 *resource / resources* 文件夹,可能与系统内部冲突,所以把名称改一下就可以;
3. 项目在运行中,没有 stop 就直接 run 可能也会导致此问题,这个问题,养成好习惯,先 stop 在 run。

参考:Xcode: Could not inspect the application package

iOS Tips


在实际开发过程中,经常会遇到一些小问题,然而,这个小问题经常会搞的你无比的郁闷。这里做个记录:

一、UIButton

1. 当按钮属性 ***enable**=NO* 时,设置按钮的 **title** 是无效的,按钮 **title** 不能修改。

iOS 7 自定义Back按钮 与 Pop Interactive Gesture 问题

1、自定义Back按钮

iOS中很多时候我们都会自定义返回按钮,也是一件easy的事,类似如下:

// 自定义返回按钮
 - (void)showNavBackButton
 {
     UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
     [backButton addTarget:self action:@selector(backButtonAction:)
          forControlEvents:UIControlEventTouchUpInside];
     [backButton setBackgroundImage:[UIImage imageNamed:@"00_back_button"]
                           forState:UIControlStateNormal];
     [backButton setTitle:kLoc(@"Back") forState:UIControlStateNormal];
     [backButton setTitleEdgeInsets:UIEdgeInsetsMake(0, 5, 1, 0)];
     backButton.titleLabel.font = kMediumFont(12);
     backButton.frame = CGRectMake(0, 0, 51, 31);
     self.navigationItem.leftBarButtonItem
     = [[UIBarButtonItem alloc] initWithCustomView:backButton];
}

但是,这样在iOS7下Pop interactive gesture就不好使了。

这里 Here 有一个解决方法。

但是,测试发现在栈中推入一个controller后,快速向左平滑,将会引起崩溃。

查看崩溃日志,发现如下信息:

nested pop animation can result in corrupted navigation bar

2、解决Pop interactive gesture问题

优化的解决方案是简单的让NavigationController自己成为响应的接受者,最好用一个UINavigationController的子类。

1)在过渡的时候禁用interactivePopGestureRecognizer

2)当新的视图控制器加载完成后再启用,建议使用UINavigationController的子类操作

// 自定义NavigationController
@interface DCBaseNavigationController ()
<
UINavigationControllerDelegate,
UIGestureRecognizerDelegate
>
@end

@implementation DCBaseNavigationController

- (void)dealloc
{
    self.interactivePopGestureRecognizer.delegate = nil;
    self.delegate = nil;

    [super dealloc];
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    if ([self respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
        self.interactivePopGestureRecognizer.delegate = self;
        self.delegate = self;
    }
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Override

- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated
{
    // Hijack the push method to disable the gesture
    if ([self respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
        self.interactivePopGestureRecognizer.enabled = NO;
    }

    [super pushViewController:viewController animated:animated];
}

#pragma mark - UINavigationControllerDelegate

- (void)navigationController:(UINavigationController *)navigationController
       didShowViewController:(UIViewController *)viewController
                    animated:(BOOL)animate
{
    if ([navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
        navigationController.interactivePopGestureRecognizer.enabled = YES;
    }
}

@end

3、Pop interactive gesture冲突,造成页面假死问题

我遇到的情况是,Push/Pop页面时,没有立即得到想要的效果,页面没有显出出来,NavigationController的didShowViewController:回调方法也没有调用。

页面布局情况是这样的:视图A,有一个Pan手势;视图B是TabBarController,其ViewControllers都是NavigationController。视图B是视图A的子视图。

后来找到原因是:navigationController的interactive pop手势与视图A的pan手势冲突。

具体原因是:rootViewController加载时,调用了didShowViewController:,设置interactivePopGestureRecognizer可用,其实我们并不需要在root的时候也触发这个手势。所以稍加优化如下:

// 优化
- (void)navigationController:(UINavigationController *)navigationController
       didShowViewController:(UIViewController *)viewController
                    animated:(BOOL)animate
{
    if ([navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
        //if ([[navigationController.viewControllers firstObject] isEqual:viewController]) {
        if ([navigationController.viewControllers count] == 1) {
            // Disable the interactive pop gesture in the rootViewController of navigationController
            navigationController.interactivePopGestureRecognizer.enabled = NO;
        } else {
            // Enable the interactive pop gesture
            navigationController.interactivePopGestureRecognizer.enabled = YES;
        }
    }
}

当前显示的是root时,设置interactivePopGestureRecognizer不可用,非root时设置interactivePopGestureRecognizer可用。

参考文章:http://keighl.com/post/ios7-interactive-pop-gesture-custom-back-button/

新的开始


我的第一篇博客, 欢迎

My first blog, Welcome ! Start a new journey !

我的第一篇博客, 开始新的旅程 !


我的第一篇博客, 欢迎大家 !