问题
In my Ipad application I use a popover but I'm not able to make it transparent. The popover has a tableview inside; my code is:
UIViewController* popoverContent = [[UIViewController alloc] init];
UIView* popoverView = [[UIView alloc] initWithFrame:CGRectMake(600, 800, 100, 100)];
popoverView.backgroundColor = [UIColor greenColor];
popoverContent.view = zoneViewController.view;
self.popoverController = [[UIPopoverController alloc] initWithContentViewController:popoverContent];
[self.popoverController presentPopoverFromRect:CGRectMake(1200, -200, 50, 375) inView:self.view permittedArrowDirections:UIPopoverArrowDirectionDown animated:TRUE];
[[[popoverController contentViewController] view] setAlpha:0.25f];
I'm able to make transparent only the tableview, but popover remains black...why??
回答1:
[[[popoverController contentViewController] view] setAlpha:0.25f];
This modifies the transparency of zoneViewController's view, which will be nested inside the Popover. However, there's currently no way for you to customize the appearance of the Popover itself. You can control the size and arrow direction, but not the border color or border transparency of the Popover.
If you really want a transparent Popover frame, you'll have to write your own.
Related: How to customize / style a UIPopoverController
回答2:
I spent hours trying every solution listed on this site to get transparency to work on a UIPopoverController
containing a GROUPED UITableView
-- NONE of which worked for me:
- Put the popover in a parent view and change the alpha on THAT view. (FAILED)
- Change the alpha on various combinations of subviews (tableview background, tableview cells, popover background, etc.)
- Add background views to the cells and change the alpha on those. (FAILED)
- Finally: Change the alpha on EVERY view in the hierarchy starting from the popover content view (FAILED)
There's a black root view in the popover that has an alpha of 1.0 and it doesn't matter what you do with the subviews (or if you stick it in a superview), the transparency doesn't work. So, I crawled up the view hierarchy (by trial and error) until I got to that cursed black view and set ITS alpha explicitly. Success! All I had to add was one line of code AFTER the popover was visible:
self.popoverController.contentViewController.view.superview.superview.superview.alpha = 0.5;
N.B. This solution will likely stop working when Apple makes updates to UIPopoverController
, so use at your own risk!
回答3:
Since iOS 5 you can set custom UIPopoverBackgroundView. Check the discussion at Customizing the UIPopoverController view background and border color
回答4:
The popover is by default transparent. If you place subviews into the Popover view, then you must make the subviews' backgroundColor
transparent.
So, in your case, you will need to set the tableView's backgroundColor
property to [UIColor clearColor]
and you will also need to make your tableView cells transparent.
Do not forget to make your cells transparent!
来源:https://stackoverflow.com/questions/5503520/popover-and-tableview-transparent