坑爹的导航栏返回键(navigationItem.backButtonBarItem)

如何自定义 navigation controller 的 pop 转场动画?

今天要自定义从 ViewController A 到 ViewControllerB 的转场( segue )动画,这两个 controller 都在一个 UInavigationController 管理之下。有 A push 到 B 的专场做好了,但是发现怎么都拦截不到点击系统自带导航栏上返回键的事件来 prepare 我自己写的 unwind segue。Google 了半个小时,找到了一个网页,标题跟我的问题一样一样的,好开心啊。结果点进去就黑线了,所有的评论都说 build-in back 事件是拦截不到的...

“I've already wasted so much time on this that I am almost tempted to
go down the route of subclassing UINavigationController, trying to
trigger a segue manually on a pop.”

”Oh. So your question is not actually about segue unwinding. That's disappointing. :)“

“If you want a button in the UINavigationBar to do something other than "go back", don't use a back button.”

好吧,那按照大家说的把 back button 换掉。StackOverFlow 都说这么干,

UIBarButtonItem *backButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"back" style:UIBarButtonItemStylePlain target:self action:@selector(backButtonPressed)];
self.navigationItem.backButtonBarItem = backButtonItem;

结果怎么都替换不掉 build-in back button,也没有的任何警告。试来试去,又半小时没了。

解决方案

正确的姿势是,将 build-in back 隐藏掉,然后添加一个 leftBarButtonItem,问题解决。

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    [self.navigationItem setLeftBarButtonItem:[[UIBarButtonItem alloc] initWithTitle:@"Library" style:UIBarButtonItemStyleDone target:self action:@selector(onLibraryButtonClicked:)]];
    self.navigationItem.hidesBackButton = YES;
}


- (IBAction)onLibraryButtonClicked:(id)sender {
    [self performSegueWithIdentifier:@"segueUnwindFromMapToCollection" sender:self];
}

The Life Cycle of a Segue

好在折腾了这么久也对 segue 的 life time 有了更深的了解。Apple官方文档如下,

To understand how custom segues work, you need to understand the life cycle of a segue object. Segue objects are instances of UIStoryboardSegue or one of its subclasses. Your app never creates segue objects directly; they are always created on your behalf by iOS when a segue is triggered. Here’s what happens:

  1. The destination controller is created and initialized.
  • The segue object is created and its initWithIdentifier:source:destination: method is called. The identifier is the unique string you provided for the segue in Interface Builder, and the two other parameters represent the two controller objects in the transition.
  • The source view controller’s prepareForSegue:sender: method is called. See Configuring the Destination Controller When a Segue is Triggered.
  • The segue object’s perform method is called. This method performs a transition to bring the destination view controller on-screen.
  • The reference to the segue object is released, causing it to be deallocated.

另外得到的两个结论:

  1. custom segue perform 方法中,destinationViewController 的 viewDidLoad: 方法是在其 view 被加到 view tree 上之后被调用的。(通常在做动画时,会先将 destinationViewController.view 作为 subView 加在前一个 sourcdViewController.view 上去。)
  2. custom segue perform 方法中,viewWillAppear: 是在 destinationViewController 被 push 之后调用的。

欢迎来我的个站逛逛: http://alexyu.me/

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 159,219评论 4 362
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,363评论 1 293
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 108,933评论 0 243
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 44,020评论 0 206
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,400评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,640评论 1 219
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,896评论 2 313
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,597评论 0 199
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,327评论 1 244
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,581评论 2 246
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,072评论 1 261
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,399评论 2 253
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,054评论 3 236
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,083评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,849评论 0 195
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,672评论 2 274
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,585评论 2 270

推荐阅读更多精彩内容

  • 我想摘一颗星朵给你 但渐渐发现这并不必要 因为在那些暗淡的夜里 你便是那颗闪耀的星朵 我想划一道彩虹给你 但渐渐发...
    诗的影子阅读 164评论 0 0
  • 冬天的阳光让人有一种说不出的开心,就像吃到一口爆浆的巧克力,闻到刚出炉的烤翅香,泡完澡可以吃到温热可口的奶茶,以及...
    ShirleyWong阅读 185评论 0 0