如何自定义 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:
- 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.
另外得到的两个结论:
- custom segue perform 方法中,destinationViewController 的 viewDidLoad: 方法是在其 view 被加到 view tree 上之后被调用的。(通常在做动画时,会先将 destinationViewController.view 作为 subView 加在前一个 sourcdViewController.view 上去。)
- custom segue perform 方法中,viewWillAppear: 是在 destinationViewController 被 push 之后调用的。
欢迎来我的个站逛逛: http://alexyu.me/
网友评论