Showing actionsheet causes CGContext invalid conte

2019-05-31 02:11发布

I'm using actionsheet to display lists of data for the user to choose from. The problem is that showing the actionsheet using [self.actionSheet showInView:self.view]; is causing several CGContext errors. The same code worked well in iOS 6.

Code:

self.actionSheet = [[UIActionSheet alloc] initWithTitle:nil
                                             delegate:nil
                                    cancelButtonTitle:nil
                               destructiveButtonTitle:nil
                                    otherButtonTitles:nil];
[self.actionSheet setActionSheetStyle:UIActionSheetStyleBlackOpaque];

CGRect tableFrame = CGRectMake(0, 40, 320, 214);
self.tableView = [[UITableView alloc] initWithFrame:tableFrame style:UITableViewStylePlain];
self.tableView.dataSource = self;
self.tableView.delegate = self;
[self.actionSheet addSubview:self.tableView];

UISegmentedControl *closeButton = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObject:@"Done"]];
closeButton.momentary = YES;
closeButton.frame = CGRectMake(260, 7.0f, 50.0f, 30.0f);
closeButton.tintColor = [UIColor redColor];
[closeButton addTarget:self action:@selector(dismissActionSheet:) forControlEvents:UIControlEventValueChanged];
[self.actionSheet addSubview:closeButton];

[self.actionSheet showFromView:self.view];

[UIView beginAnimations:nil context:nil];
[self.actionSheet setBounds:CGRectMake(0, 0, 320, 485)];
[UIView commitAnimations];

Errors:

CGContextSetFillColorWithColor: 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.
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.
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.
CGContextSetFlatness: 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.
CGContextAddPath: 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.
CGContextDrawPath: 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.
CGContextRestoreGState: 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.

Update:

The workaround to set cancelButtonTitle to @"" results in this UI problem for me:

afterbefore

The original code came from another stackoverflow answer, see https://stackoverflow.com/a/2074451/654870.

3条回答
够拽才男人
2楼-- · 2019-05-31 02:35

I believe the answer here is that UIActionSheet is not intended to be used this way, and as a result may have side effects like these. From the Apple ActionSheet documentation, "UIActionSheet is not designed to be subclassed, nor should you add views to its hierarchy."

While this didn't cause problems for me in iOS 6, something has clearly changed in iOS 7 and I am more in favor of going down another route than trying to do something that contradicts the docs. Note that a workaround may be to pass @"" for the cancelButtonTitle rather than nil, but this causes other UI problems and may not be approved by Apple.

Alternative solutions:

  1. Create your own view and present it modally - I made a simple example project showing one way to do this.
  2. https://github.com/gpambrozio/BlockAlertsAnd-ActionSheets (no update for iOS 7 yet)
查看更多
萌系小妹纸
3楼-- · 2019-05-31 02:41

Kyle, What other UI problem that you have after using @""?

After I used the following code, it works fine for me. I dont use tableview though, I use pickerview as the subview.

   self.startSheet=[[UIActionSheet alloc]initWithTitle:nil
                                           delegate:nil
                                  cancelButtonTitle:@""
                             destructiveButtonTitle:nil
                                  otherButtonTitles:nil];
查看更多
Bombasti
4楼-- · 2019-05-31 02:52

Here is an answer i gave on a similar question that works well

self.actionSheet = [[UIActionSheet alloc] initWithTitle:@""
                                               delegate:nil
                                      cancelButtonTitle:nil
                                 destructiveButtonTitle:nil
                                      otherButtonTitles:nil];

However that will mess up the graphic/drawing at the top of your action sheet so if you dont already have a background your adding just add this code to give you the default action sheet look.

UIView *background = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
background.backgroundColor = [UIColor colorWithRed:204.0/255.0 green:204.0/255.0 blue:204.0/255.0 alpha:1];
[self.actionSheet addSubview:background];

then add whatever else you want to your custom action sheet.

查看更多
登录 后发表回答