ios


Animating UISearchBar inside navigation bar with back button


This is one of the most bizarre things I've ever seen in iOS. I was working on pretty simple animation in navigation bar: I have nav bar with back button with title (normal left) and search icon (right). On search icon tap I animate nav bar so it hides (fades out) both back button and search icon but shows (fades in) search bar. On cancel button tap it does the opposite. Simple.
Well... not so much. I had the animation, everything nice, but the second time I tapped the search icon, the back button title first fell back to "Back" and with another cycle (cancel, tap search) it showed empty title (just the back arrow).
Here is the code:
private func showSearchBar() {
navigationItem.setLeftBarButton(nil, animated: true)
navigationItem.setRightBarButton(nil, animated: true)
navigationItem.setHidesBackButton(true, animated: true)
UIView.animate(withDuration: 0.1, animations: {
self.navigationItem.titleView?.alpha = 0.0
}) { _ in
let searchBar = self.searchController?.searchBar
self.navigationItem.titleView = searchBar
//self.navigationItem.titleView?.sizeToFit() //not needed
self.searchController?.searchBar.becomeFirstResponder()
searchBar?.alpha = 0.0
UIView.animate(withDuration: 0.3, animations: {
searchBar?.alpha = 1.0
}, completion: { _ in
})
}
}
fileprivate func hideSearchBar() {
UIView.animate(withDuration: 0.2, animations: {
self.navigationItem.titleView?.alpha = 0.0
}) { _ in
if self.showsMenuButton {
self.navigationItem.setLeftBarButton(self.sideMenuButton, animated: true)
}
self.navigationItem.setRightBarButton(self.searchButton, animated: true)
self.navigationItem.setHidesBackButton(false, animated: true)
self.navigationItem.titleView = nil
self.navigationItem.titleView?.alpha = 0.0
UIView.animate(withDuration: 0.3) {
self.navigationItem.titleView?.alpha = 1.0
}
}
}
I call these two methods on search icon tap and willDismissSearchController(_ ...) respectively.
Believe it or not, what makes the difference (animated or not - makes no difference) is the order in which I call showing and hiding of stuff in hideSearchBar() method. If I move line self.navigationItem.titleView = nil before showing of bar buttons, it works like a charm.
To try this you don't need the animations. Anybody has an explanation?

Related Links

Xcode Undefined symbols for architecture x86_64:
How to find the average pixel colour of a defined area in UI image
How to loop in each dictionary key and value and print in UILabel
TableViewController Detect Row Tap with Static Cells
Navigation bar shows white space
Getting yesterdays steps from HealthKit
How to transfer data from classic Bluetooth device to iOS?
swift 3.0 folding-cell library table not showing
Repeating background task Swift 3 DispatchSourceTimer [duplicate]
How to get instance of Currently Shown UItextField in Page Control
iOS - How to set nil in UITextField (It should be visible)
Xamarin Forms Image, iOS renderer - ALAssetOrientation and UIImageOrientation
Objective-C - making a Unity plugin from Aruts
Cocoapods dyld: Library not loaded #rpath Image not found
Check live if UITextField is not empty
How to compare two nsarrays with different values?

Categories

HOME
codeigniter
hadoop
embedded-linux
common-lisp
configuration
direction
localforage
streaming
encog
oracle-adf
phpmailer
python-xarray
apache-spark-sql
autofac
windows-10-desktop
telnet
wso2ei
sqlconnection
proftpd
tapi
nservicebus
highlight
asp.net-mvc-5.2
installer
spring-cloud-consul
watchservice
flow
pywin32
spring-restdocs
dotnetbar
centroid
mlr
structure
simple-html-dom
valueinjecter
python-responses
directx-12
react-redux-form
google-shared-contacts
machine-language
pymssql
sieve-of-eratosthenes
powerbuilder-conversion
perl-module
partition
response
compass-lucene
linear
gradle-tooling-api
php-5.5
qmenubar
php-deployer
freecodecamp
spring-form
mashery
android-identifiers
jvm-arguments
tfs2008
ispconfig
sound-synthesis
ellipsis
file-management
finalcut
android-number-picker
winobjc
webmail
mars
occam-pi
equinox
bonfire
abort
getuikit
conditional-statements
scribe
json-web-token
spiceworks
misra
mpj-express
lwuit-list
short
srp
jquery-mobile-popup
vlfeat
llblgenpro
micro-orm
emacs-jedi
memoization
pubdate
wescheme
clean-urls
qbwc
zmodem
undeclared-identifier
rgba
time-limiting
expressionvisitor
b-method
textboxlist
eye-detection
formsauthentication
custom-tag
wiki-markup
unitils
cassandra-0.7
executescalar
database-dump
shareware

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App