I found the issue on the end. I have tap gesture recognizer which cancelled touch events and they never reached the tabbar. Still have no idea why the CGContext error appeared.
Toolbar button selector not fired with message in console: <Error>: CGContextSaveGState: invalid context
-
19-07-2023 - |
문제
I am adding UIToolbar
programatically with the below code. It should handle basic image editing, but when I tap any button on the toolbar, I get this error message instead, and the selector is not called. The viewController is embeded in UINavigationController
and has UIScrollView
with UIImageView
.
I have found here that the error might be ignored in some cases as it is a bug in iOS, however in my situation my selector is also not called along with this message. I have also found someone get this error with zero sized CGContext and I do some image capturing with CGContext, but it is working fine and it is not related to toolbar buttons and selectors. Any suggestions?
<Error>: CGContextSaveGState: 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.
Code:
- (void)addBasicToolbar
{
// create toolbar
UIToolbar *basicToolbar = [[UIToolbar alloc] init];
basicToolbar.barStyle = UIBarStyleBlack;
basicToolbar.translucent = NO;
basicToolbar.translatesAutoresizingMaskIntoConstraints = NO;
[self.view addSubview:basicToolbar];
// add constraints
NSDictionary *metrics = @{@"barHeight":@44.0};
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"|[basicToolbar]|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(basicToolbar)]];
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[basicToolbar(==barHeight)]|" options:0 metrics:metrics views:NSDictionaryOfVariableBindings(basicToolbar)]];
// add bar buttons
UIBarButtonItem *rotateLeft = [[UIBarButtonItem alloc] initWithTitle:@"Rotate left" style:UIBarButtonItemStylePlain target:self action:@selector(rotateLeft)];
UIBarButtonItem *rotateRight = [[UIBarButtonItem alloc] initWithTitle:@"Rotate right" style:UIBarButtonItemStylePlain target:self action:@selector(rotateRight)];
UIBarButtonItem *resetButton = [[UIBarButtonItem alloc] initWithTitle:@"Revert to original" style:UIBarButtonItemStylePlain target:self action:@selector(resetToOriginal)];
UIBarButtonItem *markupButton = [[UIBarButtonItem alloc] initWithTitle:@"Add markup" style:UIBarButtonItemStylePlain target:self action:@selector(addMarkup)];
UIBarButtonItem *flexibleSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
basicToolbar.items = @[resetButton, flexibleSpace, rotateLeft, rotateRight, flexibleSpace, markupButton];
}
- (void)rotateLeft
{
NSLog(@"rotate left called"); // but is actually not called
}
해결책
제휴하지 않습니다 StackOverflow