UISearchBar CGContext ERROR

IosCocoa TouchUisearchbarCgcontext

Ios Problem Overview


I have a UISearchBar inside a view, whenever I tap on it, after the keyboard comes up -

after -(BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar

it sends this to the console:

> <Error>: CGContextSetStrokeColorWithColor: invalid context 0x0. This > is a serious error. This application, or a library it uses, is using > an invalid context and is thereby contributing to an overall > degradation of system stability and reliability. This notice is a > courtesy: please fix this problem. It will become a fatal error in an > upcoming update.

It repeats the same error. I am wonderring what exactly could be the problem?

I believe there is a NULL context out there but what it has to do with a UISearchBar? tnx.

Ios Solutions


Solution 1 - Ios

It´s a known issue on which Apple is working on. Should be fixed in the next beta release.

Have a look here: https://stackoverflow.com/questions/17080554/xcode-number-pad-with-decimal-error

Edit: For those who have that issue with a textfield maybe this should get you around:

From Apple Developer Forums bye Popeye7 - So all credits to him

> I have found a fix for this issue! I have 3 apps that this is now broken on, so, to me... this is a good find. Found the solution on StackOverflow... combined two answers to a similar question.

>In my case, a user taps a barButtonItem and an "alert" or dialog appears.

>I see the big difference is in how the UIAlertView is allocated. The "NEW WAY" has the textField showing and brings up the keyboard as it should.

>I am now able to see the textField, enter text and it works the way I expect it to. Adding the "initWithFrame" back in has no affect on the textField placement.

>OLD WAY....

- (IBAction)addEntryTapped:(id)sender

{

    [_editorTextView resignFirstResponder];
    [self saveTextChanges];
    [self dismissPopovers];

    _prompt = [[UIAlertView alloc] initWithTitle:@"New Entry Title..."
                                         message:@"\n\n\n" // IMPORTANT
                                        delegate:self
                               cancelButtonTitle:@"Cancel"
                               otherButtonTitles:@"OK", nil];

    _textField = [[UITextField alloc] initWithFrame:CGRectMake(17.0, 55.0, 250.0, 25.0)];

    [_textField setBackgroundColor:[UIColor whiteColor]];
    [_textField setPlaceholder:@"New Entry Title"];

    _textField.borderStyle = UITextBorderStyleRoundedRect;
    _textField.autocapitalizationType = UITextAutocapitalizationTypeWords;
    _textField.autocorrectionType = UITextAutocorrectionTypeNo;

    [_prompt addSubview:_textField];
    [_prompt show];

    // set cursor and show 
    [_textField becomeFirstResponder];
}

> NEW WAY...

- (IBAction) addEntryTapped:(id)sender
{
    [_editorTextView resignFirstResponder];
    [self saveTextChanges];
    [self dismissPopovers];

    _prompt = [[UIAlertView alloc] init];
    _prompt.alertViewStyle = UIAlertViewStylePlainTextInput;

    UITextField *text = [_prompt textFieldAtIndex:0];
    _textField = text;

    [_prompt setDelegate:self];
    [_prompt setTitle:@"New Entry Title..."];
    [_prompt setMessage:@""];
    [_prompt addButtonWithTitle:@"Cancel"];
    [_prompt addButtonWithTitle:@"OK"];
    [_textField setPlaceholder:@"New Entry Title"];

    _textField.autocapitalizationType = UITextAutocapitalizationTypeWords;
    _textField.autocorrectionType = UITextAutocorrectionTypeNo;

    [_prompt show];

    // set cursor and show keyboard
    [_textField becomeFirstResponder];
}  

> Message was edited by Popeye7 on 9/25/13 at 12:25 PM

> Message was edited by Popeye7 on 9/25/13 at 12:33 PM

Solution 2 - Ios

This went away for me after deleting the iOS Simulator preferences from ~/Library/Preferences.

Go to ~/Library/Preferences Drop "com.apple.iphonesimulator.plist" to the trash.

Stateful

Solution 3 - Ios

It appears that the UISearchBar's AutoLayout constraints that are set in the .xib file are causing this problem. If there are any redundant or conflicting constraints that weren't caught by the compiler it can cause a drawing error and throw these errors.

  1. Go to the .xib file that has the UISearchBar
  2. Click on the UISearchBar and go to the Size Inspector (looks like a ruler, usually on the right side with the control's properties)
  3. One by one, click on each constraint and watch to see where they are - no two constraints should be measuring the same property. For example, on mine, I had one constraint measuring from the top of the control to the top of the view, and another constraint measuring from the bottom of the control to the top of the view.
  4. Remove any offending constraints, build, and run! If that doesn't work, you may want to check the constraints on the surrounding controls.

Solution 4 - Ios

This "missing context" thing seems to be a iOS 7 bug and it seems to occur only for empty textfields. I am using a lightweight workaround in my projects until this issue got fixed by Apple (so probably never ;)).

// or wherever
- (void)viewDidLoad
{
     if([textfield.text isEqualToString:@""] || textfield.text == nil)
     {
           textfield.text = @" ";
     }
     ...
}

- (BOOL)textFieldShouldBeginEditing:(UITextField *)theTextField
{
     [[NSNotificationCenter defaultCenter] addObserver:self
                                              selector:@selector(keyboardDidShow:)
                                                  name:UIKeyboardDidShowNotification
                                                object:nil];
     return YES;
}

- (void)keyboardDidShow:(NSNotification *)notification
{
    if([textField.text isEqualToString:@" "])
    {
         textField.text = @"";
    }
}

...that did it for me.

EDIT: You need to implement UITextFieldDelegate, of course. EDIT 2: Unfortunately, it didn't work exactly as I expected. The error is gone, but the whitespace-character is not removed most of the time...Anyone got a solution for this? EDIT 3: I am giving up on that issue. This way does not cover up all use cases of UITextField and it decreases the UX.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionhenTdevView Question on Stackoverflow
Solution 1 - IosHenning SchulzView Answer on Stackoverflow
Solution 2 - IosStatefulView Answer on Stackoverflow
Solution 3 - IosChristineView Answer on Stackoverflow
Solution 4 - Iosmax.mustermannView Answer on Stackoverflow