坑爹的導航欄返回鍵(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:
- 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/
相關文章
- 談導航欄返回按鈕的替代方案
- iOS隱藏導航欄的返回按鈕iOS
- 自定義導航欄返回按鈕導致返回手勢失效問題
- iOS 導航欄的控制iOS
- iOS開發:給UIWebview的導航欄新增返回、關閉按鈕iOSUIWebView
- WordPresscategory導航欄Go
- 坑爹的Sun JDKJDK
- qml 導航欄TabBar 工具欄ToolBartabBar
- GitHub 導航欄加強Github
- HTML橫向導航欄HTML
- Flutter 導航欄AppBarFlutterAPP
- Tablayout實現導航欄TabLayout
- bootstrap導航欄學習boot
- 坑爹的驗證碼。。
- 坑爹的Oracle 11.2.0.3Oracle
- 坑爹的IE8
- 坑爹的Python陷阱(避坑指南)Python
- uniapp自定義導航欄APP
- 微信小程式自定義導航欄微信小程式
- CSS導航欄及下拉選單CSS
- ios自定義簡約導航欄iOS
- CSS 導航欄元素居中顯示CSS
- 常用元件 / 導航欄及圖示元件
- iOS導航欄佈局相關iOS
- 十二個坑爹的Python陷阱Python
- android 支援鍵盤導航Android
- 直播系統平臺原始碼隱藏虛擬導航欄,禁止下拉通知欄,禁止上滑出虛擬導航欄原始碼
- 定製化你的ReactNative底部導航欄React
- 純CSS的導航欄Tab切換例項CSS
- JS寫的二級導航欄(利用冒泡原理)JS
- iOS狀態列和導航欄的設定iOS
- JS如何實現導航欄的智慧浮動JS
- 如何設計出優秀的 App 導航欄APP
- UIView顯示時遮擋導航欄的方法UIView
- weex沉浸式導航欄解決方案
- jQuery實現吸頂動畫導航欄jQuery動畫
- CARDS主題 & 導航欄樣式修改
- uniapp更改導航欄按鈕文字APP