I'm using the delegation pattern for my CoreBluetooth based app. I have a main ViewController that is a delegate to my BLEHandler class. I'm updating a button based on the response I get from following delegate method:
func acIsOn(error: NSError?) {
if error == nil{
pushButton.setImage(UIImage(named: "state4"), for: .normal)
}
}
It works fine when my delegate controller class is in the foreground but when I move to another ViewController and calls a method of handler class that in response calls the delegate method above, the button on the image is not updated.
Here's what I have already tried:
Wrapping the statement in DispatchQueue.main.async{}
Calling pushButton.setNeedsLayout() and setNeedsDisplay()
But none of it worked. ,
Also I made sure that this method was being called when the ViewController is not in the foreground.
Edit 1: I'm more interested in learning about the limitation that is not allowing this to happen, I'm not looking for hacks/tricks to bypass this.
Edit 2: As mentioned by Shoazab, button.setBackgroundImage() is working when the VC is in background. Still curious why button.setImage doesn't work in background but it does in when VC is on top.
You are trying to changed an UI element on a ViewController which is not currently displayed. It will update values but no refresh on UI will be executed.
I think that calling SetNeedsDisplay on method viewWillAppear on your ViewController will fix your problem.
You can also use a variable Image and update the button Image when the controller is displayed again.
Move the UI update code to the viewWillAppear like this:
class MainViewController: UIViewController, BLEHandler {
var isUIUpdateNeeded = false
//Define your UI outlets or proeprties
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
override func viewWillAppear(_ animated: Bool) {
if isUIUpdateNeeded {
updateUI()
}
}
func acIsOn(error: NSError?) {
if error == nil{
pushButton.setImage(UIImage(named: "state4"), for: .normal)
}
}
func updateUI() {
//do your UI changes here
pushButton.setImage(UIImage(named: "state4"), for: .normal)
}
}
Call btn.setBackgroundImage() it will set it
I subclassed UICollectionViewLayout in order to create a calendar. I would like to give users the ability to change some settings like the number of days to be shown on the screen (7 by default).
I save daysToShow in UserDefaults. Whenever the UIStepper value is changed it calls this method:
func stepperValueChanged(sender:UIStepper){
stepperValue = String(Int(sender.value))
valueLabel.text = String(Int(sender.value))
UserDefaults.standard.set(String(Int(sender.value)), forKey: "daysToShow")
NotificationCenter.default.post(name: Notification.Name(rawValue: "calendarSettingsChanged"), object: nil, userInfo: nil)
}
So after I save the new value in UserDefault, I post a notification which then calls reloadForSettingsChange (which is actually getting called as I set a breakpoint here):
override func viewDidLoad() {
super.viewDidLoad()
NotificationCenter.default.addObserver(self, selector: #selector(reloadForSettingsChange(notification:)), name: NSNotification.Name(rawValue: "calendarSettingsChanged"), object: nil)
// other code.....
}
func reloadForSettingsChange(notification:NSNotification){
// here I save the user setting in a variable declared in my custom UICollectionViewLayout
self.calendarView.daysToShow = Int(UserDefaults.standard.string(forKey: "daysToShow")!)
self.calendarView.daysToShowOnScreen = Int(UserDefaults.standard.string(forKey: "daysToShow")!)
self.calendarView.forceReload(reloadEvent: true)
}
func forceReload(reloadEvent:Bool){
DispatchQueue.main.async {
if reloadEvent{
self.groupEventsByDays()
self.weekFlowLayout?.invalidateCacheLayout()
self.collectionView.reloadData()
}
}
}
func invalidateCacheLayout(){
self.needsToPopulateAttributesForAllSections = true
self.cachedDayDateComponents?.removeAllObjects()
self.cachedStartTimeDateComponents?.removeAllObjects()
self.cachedEndTimeDateComponents?.removeAllObjects()
self.cachedCurrentDateComponents?.removeAllObjects()
self.cachedEarliestHour = Int.max
self.cachedLatestHour = Int.min
self.cachedMaxColumnHeight = CGFloat.leastNormalMagnitude
self.cachedColumnHeights?.removeAllObjects()
self.cachedEarliestHours?.removeAllObjects()
self.cachedLatestHours?.removeAllObjects()
self.itemAttributes?.removeAllObjects()
self.allAttributes?.removeAllObjects()
_layoutAttributes.removeAll()
self.invalidateLayout()
}
The problem is that the layout is not being updated (invalidated?) until I rotate the screen device (i.e from landscape to portrait). The function I use for the rotation calls exactly the same method I call in reloadForSettingsChange so I don't understand why it works when I rotate the screen and not before:
func rotated() {
switch UIDevice.current.orientation {
case .landscapeLeft, .landscapeRight:
calendarView.forceReload(reloadEvent: true)
default:
calendarView.forceReload(reloadEvent: true)
}
}
I found the solution: I am now calling setNeedsLayout():
func reloadForSettingsChange(notification:NSNotification){
self.calendarView.daysToShow = Int(UserDefaults.standard.string(forKey: "daysToShow")!)
self.calendarView.daysToShowOnScreen = Int(UserDefaults.standard.string(forKey: "daysToShow")!)
self.calendarView.forceReload(reloadEvent: true)
self.calendarView.setNeedsLayout() // this line has been added
}
I had initially used layoutSubviews() which worked but Apple Documentation says:
You should not call this method directly. If you want to force a
layout update, call the setNeedsLayout() method instead to do so prior
to the next drawing update. If you want to update the layout of your
views immediately, call the layoutIfNeeded() method.
I tried setNeedsLayout() which seemed to be the right method to call (?!?) but since it wasn't working I used setNeedsLayout():
Call this method on your application’s main thread when you want to
adjust the layout of a view’s subviews. This method makes a note of
the request and returns immediately. Because this method does not
force an immediate update, but instead waits for the next update
cycle, you can use it to invalidate the layout of multiple views
before any of those views are updated. This behavior allows you to
consolidate all of your layout updates to one update cycle, which is
usually better for performance.
As I understand, the default behaviour of UISearchController is:
On tapping search bar, background is dimmed and 'cancel' button is shown. SearchResultsController is not shown till this point.
SearchResultsController is displayed only if search bar is not empty.
I want to display SearchResultsController even when search bar is empty but selected (i.e is case 1 above).
Simply put, instead of background dimming, I would like to show Search results.
Is there a way for doing this?
More Clarification:
I am not using UISearchController to filter results shown on the view on which it is shown, but some other unrelated results.
It will be like what facebook does on its 'News Feed'. Tapping on search bar shows search suggestions initially and then, when we start editing, it shows search results which might not be related to news feed.
You can simply implement the UISearchResultsUpdating protocol and set the results controller view to always show in updateSearchResultsForSearchController:
func updateSearchResultsForSearchController(searchController: UISearchController) {
// Always show the search result controller
searchController.searchResultsController?.view.hidden = false
// Update your search results data and reload data
..
}
This works because the method is called even when the search bar is activated, without any text.
If your searchBar is active but has no text, the underlying tableView results are shown. That's the built-in behavior, and the reason why searchResultsController is hidden for that state.
To change the behavior when search is active but not filtering, you're going to have to show the searchResultsController when it is normally still hidden.
There may be a good way to accomplish this via <UISearchResultsUpdating> and updateSearchResultsForSearchController:. If you can solve it via the protocol, that's the preferred way to go.
If that doesn't help, you're left with hacking the built-in behavior. I wouldn't recommend or rely on it, and it's going to be fragile, but here's an answer if you choose that option:
Make sure your tableViewController conforms to <UISearchControllerDelegate>, and add
self.searchController.delegate = self;
Implement willPresentSearchController:
- (void)willPresentSearchController:(UISearchController *)searchController
{
dispatch_async(dispatch_get_main_queue(), ^{
searchController.searchResultsController.view.hidden = NO;
});
}
This makes the searchResultsController visible after its UISearchController set it to hidden.
Implement didPresentSearchController:
- (void)didPresentSearchController:(UISearchController *)searchController
{
searchController.searchResultsController.view.hidden = NO;
}
For a better way to work around the built-in behavior, see malhal's answer.
Updated for iOS 13
From iOS13, we got system API support for this behaviour. You can set the property showsSearchResultsController = true
For iOS 12 and below
I am recently working on UISearchController. I want to show search history in searchResultsController when search bar is empty. So searchResultsController needs to show up whenever UISearchController gets presented.
Here, I use another solution to make the searchResultsController always visible by overriding the hidden property in a custom view.
for example, my searchResultsController is a UITableViewController. I create a VisibleTableView as a subclass of UITableView, and then change the UITableView custom class of searchResultsController to VisibleTableView in xib or storyboard. This way, my searchResultsController will never be hidden by UISearchController.
The good things here:
Easier to implement than KVO.
No delay to show searchResultsController. Flipping the hidden flag in "updateSearchResults" delegate method works, but there is a delay to show the searchResultsController.
It does't reset the hidden flag, so there is no UI gap/jumping between hidden and visible.
Swift 3 sample code:
class VisibleTableView: UITableView {
override var isHidden: Bool {
get {
return false
}
set {
// ignoring any settings
}
}
}
I have tried PetahChristian solution, the preload result did show up when we first focus the searchbar, but when we enter something then clear it, the preload results will not reappear.
I came up with another solution. We only need to add a delegate into SearchResultsController and call it when our searchController.searchBar.text is empty. Something like this:
SearchResultsController:
protocol SearchResultsViewControllerDelegate {
func reassureShowingList() -> Void
}
class FullSearchResultsViewController: UIViewController, UISearchResultsUpdating{
var delegate: SearchResultsViewControllerDelegate?
...
func updateSearchResultsForSearchController(searchController: UISearchController) {
let query = searchController.searchBar.text?.trim()
if query == nil || query!.isEmpty {
...
self.delegate?.reassureShowingList()
...
}
...
}
And in the controller contains the SearchController, we add our delegate:
self.searchResultsController.delegate = self
func reassureShowingList() {
searchController.searchResultsController!.view.hidden = false
}
With tricky things like this I recommend the sledge hammer approach! That is to detect when something tries to make it hidden and when it does, change it back. This can be done via KVO (Key Value Observing). This will work no matter what, without having to handle all the intricacies of the search bar. Sorry the code is complicated but KVO is an older style API but my code follows recommend practice. In your SearchResultsViewController put this:
static int kHidden;
#implementation SearchResultsViewController
-(void)viewDidLoad{
[super viewDidLoad];
[self.view addObserver:self
forKeyPath:#"hidden"
options:(NSKeyValueObservingOptionNew |
NSKeyValueObservingOptionOld)
context:&kHidden];
}
- (void)observeValueForKeyPath:(NSString *)keyPath
ofObject:(id)object
change:(NSDictionary *)change
context:(void *)context {
// if it was our observation
if(context == &kHidden){
// if the view is hidden then make it visible.
if([[change objectForKey:NSKeyValueChangeNewKey] boolValue]){
self.view.hidden = NO;
}
}
else{
// if necessary, pass the method up the subclass hierarchy.
if([super respondsToSelector:#selector(observeValueForKeyPath:ofObject:change:context:)]){
[super observeValueForKeyPath:keyPath
ofObject:object
change:change
context:context];
}
}
}
- (void)dealloc
{
[self.view removeObserver:self forKeyPath:#"hidden"];
}
// Here have the rest of your code for the search results table.
#end
This works in all cases including if the text is cleared.
Lastly, to prevent the table doing an ugly fade to grey then to white when the search activates, use this:
self.searchController.dimsBackgroundDuringPresentation = NO;
Swift 3 Version:
If your searchResultController is not nil and you are using a separate table view controller to show the search results, then you can make that table view controller conform to UISearchResultUpdating and in the updateSearchResults function, you can simply unhide the view.
func updateSearchResults(for searchController: UISearchController) {
view.hidden = false
}
Swift 4 Version:
func updateSearchResults(for searchController: UISearchController) {
view.isHidden = false
}
What is being hidden is the search results controller's view. Therefore it is sufficient to unhide it any time it might be hidden. Simply do as follows in the search results controller:
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
self.view.isHidden = false
}
func updateSearchResults(for searchController: UISearchController) {
self.view.isHidden = false
// ... your other code goes here ...
}
Now the results view (i.e. the table view) is always visible, even when the search bar text is empty.
By the way, the iOS Mail app behaves like this, and I assume that's how it's implemented (unless Apple has access to some secret private UISearchController setting).
[Tested in iOS 10 and iOS 11; I didn't test on any earlier system.]
The Swift 2.3 version of #malhal's approach:
class SearchResultsViewController : UIViewController {
var context = 0
override func viewDidLoad() {
super.viewDidLoad()
// Add observer
view.addObserver(self, forKeyPath: "hidden", options: [ .New, .Old ], context: &context)
}
deinit {
view.removeObserver(self, forKeyPath: "hidden")
}
override func observeValueForKeyPath(keyPath: String?, ofObject object: AnyObject?, change: [String : AnyObject]?, context: UnsafeMutablePointer<Void>) {
if context == &self.context {
if change?[NSKeyValueChangeNewKey] as? Bool == true {
view.hidden = false
}
} else {
super.observeValueForKeyPath(keyPath, ofObject: object, change: change, context: context)
}
}
}
Swift 4 version of malhals answer:
class SearchController: UISearchController {
private var viewIsHiddenObserver: NSKeyValueObservation?
override func viewDidLoad() {
super.viewDidLoad()
viewIsHiddenObserver = self.searchResultsController?.view.observe(\.hidden, changeHandler: { [weak self] (view, _) in
guard let searchController = self else {return}
if view.isHidden && searchController.searchBar.isFirstResponder {
view.isHidden = false
}
})
}
}
Please note the [weak self]. Otherwise you would introduce a retain cycle.
I think you are mistaken.
SearchResultsController only appears when there are results. This is slightly different than your interpretation.
The results are loaded manually based on the text in the search bar. So you can intercept it if the search bar is empty and return your own set of results.
If you don't want to dim the results, set the dimsBackgroundDuringPresentation property to false.
This will make sure that the underlying content is not dimmed during a search.
You will also have to make sure you return results even when the searchText is empty otherwise an empty tableview will be displayed.
I spent a lot of time with this, and ultimately the solution I went with is like #malhals's, but the amount of code is significantly reduced by using facebook's KVOController: https://github.com/facebook/KVOController . Another advantage here is that if your searchResultsController is a UINavigationController then you don't need to subclass it just to add #malhal's code.
// always show searchResultsController, even if text is empty
[self.KVOController observe:self.searchController.searchResultsController.view keyPath:#"hidden" options:NSKeyValueObservingOptionNew block:^(id observer, UIView* view, NSDictionary *change) {
if ([change[NSKeyValueChangeNewKey] boolValue] == YES) {
view.hidden = NO;
}
}];
self.searchController.dimsBackgroundDuringPresentation = NO;
The easiest way is to use ReactiveCocoa with this extension https://github.com/ColinEberhardt/ReactiveTwitterSearch/blob/master/ReactiveTwitterSearch/Util/UIKitExtensions.swift
presentViewController(sc, animated: true, completion: {
sc.searchResultsController?.view.rac_hidden.modify({ value -> Bool in
return false
})
} )
where sc is your UISearchController
I really liked Simon Wang's answer and worked with it and this is what I did and it works perfectly:
I subclass the UISearchController in my custom class:
class CustomClass: UISearchController {
override var searchResultsController: UIViewController? {
get {
let viewController = super.searchResultsController
viewController?.view.isHidden = false
return viewController
}
set {
// nothing
}
}
}
Also make sure you don't have this anywhere in your code:
self.resultsSearchController.isActive = true
resultsSearchController is my UISearchController
Simply what I was using this case
func updateSearchResults(for searchController: UISearchController) {
if let inputText = searchController.searchBar.text, !inputText.isEmpty {
self.view.isHidden = false
}
}
where self.view is a view of "searchResultsController" during initialisation of UISearchController.
var searchController = UISearchController(searchResultsController: searchResultsController)
I'm trying to animate the change of an image in a UIImageView using transitionWithVIew. The image changes, but it doesn't seem to be animating. Not sure why this is happening.
Here's my code:
func changeBackgroundAtIndex(index : Int) {
switch index {
case 0:
animateChangeWithImage(MLStyleKit.imageOfAboutMe)
case 1:
animateChangeWithImage(MLStyleKit.imageOfProjects)
case 2:
animateChangeWithImage(MLStyleKit.imageOfSkills)
case 3:
animateChangeWithImage(MLStyleKit.imageOfEducation)
case 4:
animateChangeWithImage(MLStyleKit.imageOfTheFuture)
default:
animateChangeWithImage(MLStyleKit.imageOfAboutMe)
}
}
func animateChangeWithImage(image : UIImage) {
UIView.transitionWithView(backgroundImageView, duration: 0.4, options: UIViewAnimationOptions.TransitionCrossDissolve, animations: { () -> Void in
self.backgroundImageView.image = image
}, completion: nil)
}
Any ideas? Thanks :)
Change your function signature to this, where you take both the image you want to change and the UIImageView itself.
func animateChangeWithImage(image: UIImage, inImageView: UIImageView)
The problem was with index. I was getting the index as the uses was scrolling through a scroll view, which would constantly keep updating the index variable and not give it enough time to actually animate.
Putting changeBackgroundAtIndex inside scrollViewDidEndDecelerating instead of scrollViewDidScroll fixed it.
It could be that the actual image itself is not animated when changed given that it is not a UIView.
This would seem to do what you want: How to animate the change of image in an UIImageView?
I have been pulling my hair out trying to get this 'Delegate' thing to work in Swift for an App I am working on.
I have two files: CreateEvent.swift and ContactSelection.swift, where the former calls the latter.
CreateEvent's contents are:
class CreateEventViewController: UIViewController, ContactSelectionDelegate {
/...
var contactSelection: ContactSelectionViewController = ContactSelectionViewController()
override func viewDidLoad() {
super.viewDidLoad()
/...
contactSelection.delegate = self
}
func updateInvitedUsers() {
println("this finally worked")
}
func inviteButton(sender: AnyObject){
invitedLabel.text = "Invite"
invitedLabel.hidden = false
toContactSelection()
}
/...
func toContactSelection() {
let contactSelection = self.storyboard?.instantiateViewControllerWithIdentifier("ContactSelectionViewController") as ContactSelectionViewController
contactSelection.delegate = self
self.navigationController?.pushViewController(contactSelection, animated: true)
}
ContactSelection's contents are:
protocol ContactSelectionDelegate {
func updateInvitedUsers()
}
class ContactSelectionViewController: UITableViewController {
var delegate: ContactSelectionDelegate?
override func viewDidLoad() {
super.viewDidLoad()
self.delegate?.updateInvitedUsers()
}
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
// Stuff
self.delegate?.updateInvitedUsers()
}
}
What am I doing wrong? I am still new and don't fully understand this subject but after scouring the Internet I can't seem to find an answer. I use the Back button available in the Navigation Bar to return to my CreateEvent view.
var contactSelection: ContactSelectionViewController = ContactSelectionViewController()
This is instantiating a view controller directly, and the value never gets used. Since it looks like you're using storyboards, this isn't a good idea since none of the outlets will be connected and you'll get optional unwrapping crashes. You set the delegate of this view controller but that's irrelevant as it doesn't get used.
It also isn't a good idea because if you do multiple pushes you'll be reusing the same view controller and this will eventually lead to bugs as you'll have leftover state from previous uses which might give you unexpected outcomes. It's better to create a new view controller to push each time.
In your code you're making a brand new contactSelection from the storyboard and pushing it without setting the delegate.
You need to set the delegate on the instance that you're pushing onto the navigation stack.
It's also helpful to pass back a reference in the delegate method which can be used to extract values, rather than relying on a separate reference in the var like you're doing.
So, I'd do the following:
Remove the var contactSelection
Add the delegate before pushing the new contactSelection object
Change the delegate method signature to this:
protocol ContactSelectionDelegate {
func updateInvitedUsers(contactSelection:ContactSelectionViewController)
}
Change your delegate calls to this:
self.delegate?.updateInvitedUsers(self)