UIPopoverController backgroundColor causing a flash when displayed

I use the new (in iOS 7) UIPopoverController.backgroundColor parameter in my application to change the color of my popovers as necessary, but I find that using this parameter causes a β€œflash” color change every time I open my popovers - after about half a second it starts with the default of translucent white and disappears to the color that I choose. This is undesirable; it should be only the color that I set when I open it.

The documentation states:

Use this property to adjust the background color of your popover. Changing the value of this property when a popover is visible causes an animated switch to a new color. The default value of this property is nil, which corresponds to the default background color.

However, even if I installed it when my application opens and does not install it again, it will flash every time you open any of the popovers.

I am open to using UIPopoverBackgroundView, but I'm not sure that it allows me to change the background color "on the fly", because it seems like a static solution for simply changing the style of all popovers in the application. Thank you in advance for any suggestions.

Change (code):

When my main view controller is loaded and ready to work with the rest of the user interface (this is one of many popover elements):

fileOptionsController = [[FileOptionsViewController alloc] initWithNibName:@"FileOptionsViewController" bundle:nil]; fileOptionsController.delegate = self; self.fileOptionsPopoverController = [[UIPopoverController alloc] initWithContentViewController:fileOptionsController]; [popoverControllers addObject:self.fileOptionsPopoverController]; 

After my popovers are initialized, I run this (still in the main initialization code) to test with a long delay between setting backgroundColor and the interaction (note: changing the alpha has no effect, it still happens when set to 1):

 for (UIPopoverController *po in popoverControllers) { [po setBackgroundColor:[UIColor colorWithWhite:0.3f alpha:0.90f]]; } 

This is then called when the user clicks a button to show a popover:

 - (void)showPopover:(UIPopoverController *)popover from:(UIButton *)btn { [popover presentPopoverFromRect:CGRectMake(btn.frame.origin.x + 5.0f, btn.frame.origin.y - 1.0f, btn.frame.size.width, btn.frame.size.height) inView:btn.superview permittedArrowDirections:UIPopoverArrowDirectionLeft animated:YES]; } 

Pretty straight forward. These are the only relevant places that are accessed by this or any popover, unless it is rejected if it is already showing.

+6
source share
3 answers

I solved this problem by rejecting the popover before changing the backgroundColor and then introducing it again:

 [popover dismissPopoverAnimated:NO]; if ([popover respondsToSelector:@selector(backgroundColor)]) { popover.backgroundColor = [UIColor someColor]; } [popover setContentViewController:viewController animated:NO]; [popover setPopoverContentSize:CGSizeMake(320.0f, 480.0f) animated:NO]; [popover presentPopoverFromRect:popoverRect inView:self.view permittedArrowDirections:UIPopoverArrowDirectionRight animated:NO]; 

The execution of this display is displayed again and you will not see any unwanted transition effect.

+2
source

Since the documentation says that backgroundColor will animate it with its own color, so it seems like it is invoking this flash animation (Apple should have given setBackgroundColor with / without animation).

If you can afford to imagine a popover without animation, I would suggest you try disabling the animation when you present it. Like one of these two -

Option 1 -

 // last parameter as 'NO' [popover presentPopoverFromRect:CGRectMake(btn.frame.origin.x + 5.0f, btn.frame.origin.y - 1.0f, btn.frame.size.width, btn.frame.size.height) inView:btn.superview permittedArrowDirections:UIPopoverArrowDirectionLeft animated:NO]; // Don't animate to present 

Option 2 - (use the setAnimationsEnabled method to temporarily disable animation)

 - (void)showPopover:(UIPopoverController *)popover from:(UIButton *)btn { [UIView setAnimationsEnabled:NO]; // Turn off animation [popover presentPopoverFromRect:CGRectMake(btn.frame.origin.x + 5.0f, btn.frame.origin.y - 1.0f, btn.frame.size.width, btn.frame.size.height) inView:btn.superview permittedArrowDirections:UIPopoverArrowDirectionLeft animated:YES]; [UIView setAnimationsEnabled:YES]; // Turn back on } 

Logically, this should work if Apple has not yet cracked this property within it sdk.

+1
source

It would be better if you added your code ... Without the code, I can just say that if you set a color after viewing, it will be animated for that color. Therefore, first change the background color and make visible.

0
source

Source: https://habr.com/ru/post/954363/


All Articles