Question

I have a keyboard accessory view that I'm attaching to the keyboard in my app in order to provide a switchable alternative input view. Everything has been working fine but during the process of fine-tuning performance for the app I realised that I was creating hundreds of copies of the accessory view when only one was really needed. So I implemented a simple Singleton pattern.

+ (v2KeyboardAccessory*) sharedInstance
{
    static dispatch_once_t pred;
    static v2KeyboardAccessory* theObject = nil;

    dispatch_once(&pred, ^{ theObject = [[self alloc] init]; });
    return theObject;
}

During the creation of my subviewcontroller (it's complicated) I set the accessory view for the textview and keep a reference (for convenience).

v2KeyboardAccessory *newAccessory = [v2KeyboardAccessory sharedInstance];
self.inputAccessory = newAccessory;
self.textView.inputAccessoryView = newAccessory.view;

In the textViewShouldBeginEditing method of my subviewcontroller I set the subviewcontroller as the current delegate (for a few key methods like switching the input view and handling undo processes).

-(BOOL)textViewShouldBeginEditing:(UITextView *)textView
{
    self.inputAccessory.delegate = self;
    self.inputAccessory.undoManager = self.textView.undoManager;
    // Some code to set which input view to display initially, etc
    return YES;
}

The only other change I can think of is that I moved some code from the keyboard accessory viewcontroller's viewDidLoad method to its viewWillAppear method. Basically it just fixes the height of the view to 44.

The problem now is the first (and only the first) time the keyboard with accessory view is displayed the keyboard itself is fine but the accessory view is not shown on the screen and the keyboard's background (grey semi-transparent for iOS7) fills all of the screen above the keyboard itself. I suspect that for some reason the accessory is being drawn just above the top of the screen and that the keyboard is filling in the rest of the space but I can't see that in my debugging.

The accessory was working perfectly prior to my implementing the singleton pattern. If I rotate the iPad while the keyboard is up it redraws itself perfectly. Every time the keyboard displays after that it works perfectly.

Was it helpful?

Solution

Turns out writing questions here is a very cathartic process! The problem related to the setting of the frame in viewWillAppear instead of viewDidLoad. Moved the frame setting code back to viewDidLoad (leaving the rest of viewWillAppear) sorted the problem out.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top