In my .plist file, I have "View controller-based status bar appearance" set to NO. But after UIImagePickerController, my app behaves as if the option is set to YES.
In my app, I present a VC that presents a UIImagePickerController.
The problem happens like this:
After photo picker is presented, when a photo library is picked, the color of the status bar text changes.
Then once, UIImagePickerController is dismissed, status bar spacing
changes for the rest of my app and all the navigation bar for other controllers displays under the status bar.
Is there a way to solve this without managing status bar in my view controllers?
None of the solutions above worked for me, but by combining Rich86man's and iOS_DEV_09's answers I've got a consistently working solution:
UIImagePickerController* imagePicker = [[UIImagePickerController alloc] init];
imagePicker.delegate = self;
and
- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated
{
[[UIApplication sharedApplication] setStatusBarHidden:YES];
}
Regarding this awesome solution. For 2014 / iOS8 I found in some cases you need to ALSO include prefersStatusBarHidden and, possibly, childViewControllerForStatusBarHidden So...
-(void)navigationController:(UINavigationController *)navigationController
willShowViewController:(UIViewController *)viewController
animated:(BOOL)animated
{
[[UIApplication sharedApplication] setStatusBarHidden:YES];
}
-(BOOL)prefersStatusBarHidden // iOS8 definitely needs this one. checked.
{
return YES;
}
-(UIViewController *)childViewControllerForStatusBarHidden
{
return nil;
}
-(void)showCamera
{
self.cameraController = [[UIImagePickerController alloc] init];
self.cameraController.delegate = (id)self; // dpjanes solution!
etc...
I faced this same issue today. Here is my solution.
In the view controller who calls the image picker, set yourself as the delegate of the image Picker. (You're probably already doing this)
UIImagePickerController* imagePicker = [[UIImagePickerController alloc] init];
imagePicker.delegate = self;
Since UIImagePickerController is a type of Navigation controller, you're also setting yourself as the UINavigationController delegate. Then :
- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated
{
[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
}
Replace UIStatusBarStyleLightContent with whatever style you are looking for.
The accepted answer will work if you have the 'View controller-based status bar appearance' set to NO in your .plist file. If indeed you need to control the status bar in some other view controllers and have this option set to YES, the other way to make UIImagePickerController to behave correctly is by subclassing it
// .h
#interface MYImagePickerController : UIImagePickerController
#end
// .m
#implementation MYImagePickerController
- (UIStatusBarStyle)preferredStatusBarStyle
{
return UIStatusBarStyleLightContent; // change this to match your style
}
#end
i faced the same problem.
here is my solution.
put this in the viewWillAppear of the view controller from which you are opening the image pickerview
-(void) viewWillAppear:(BOOL)animated{
[super viewWillAppear:YES];
[[UIApplication sharedApplication] setStatusBarHidden:YES];
}
Can you try this. I think needsStatusBarApperanceUpdate will work.
1 -Set UIViewControllerBasedStatusBarAppearance to NO.
2- Call [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
3- [self setNeedsStatusBarAppearanceUpdate];
I found this to offer proper handling, there's two parts.
- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated
{
[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleBlackOpaque];
}
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleBlackOpaque];
...
the UIImagePickerController itself presents view controllers, so this delegate works for all presenters on the stack.
the viewWillAppear ensures this view controller itself is always reset whenever a presenting view controller dismisses above it.
I had the same problem.
Add in info plist: "View controller-based status bar appearance" with value "NO"
Example here https://stackoverflow.com/a/19211669
This solution works for me.
This is probably a bug. I solved the problem by setting "View controller-based status bar appearance" set to YES and in every view controller pasting in the following code:
- (BOOL)prefersStatusBarHidden
{
return YES;
}
Then my app behaves as expected.
For hiding the status bar in UIImagePicker :
-
(void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated
{
[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
}
and when UIImagePicker is dismissed to hide the status bar in View controller use the following code :
-(void) viewWillAppear:(BOOL)animated{
[super viewWillAppear:YES];
[[UIApplication sharedApplication] setStatusBarHidden:YES];
}
try this ....
this will work in both cases i.e whether you use presentModalViewController and pushViewController
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
delegate methods
-(void)imagePickerController:(UIImagePickerController*)picker didFinishPickingMediaWithInfo:(NSDictionary*)info
{
[[UIApplication sharedApplication] setStatusBarHidden:YES];
[picker dismissViewControllerAnimated:YES completion:^{}];
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
[[UIApplication sharedApplication] setStatusBarHidden:YES];
[picker dismissViewControllerAnimated:YES completion:nil];
}
All the above didn't work for me. I solved the issue by changing the presentation style to:
imagePickerController.modalPresentationStyle = UIModalPresentationFullScreen;
None of the above solutions worked for me.
I present UIImagePickerController as modal view controller. After dismissing UIImagePickerController the status bar state was:
[UIApplication sharedApplication].statusBarOrientation = 0 (UIDeviceOrientationUnknown)
[UIApplication sharedApplication].statusBarFrame = { 0, 0, 0, 0}
The solution that fixed the problem for me was restoring statusBarOrientation after dismissing UIImagePickerController:
UIImagePickerController *cameraUI = [[UIImagePickerController alloc] init];
[self.viewController presentViewController:cameraUI animated:true completion:^(void){ }];
...
[self.viewController dismissViewControllerAnimated:animated completion:^(void){
[UIApplication sharedApplication].statusBarOrientation = UIInterfaceOrientationPortrait;
}];
This code helped me to customize status bar style.
EDIT: this solution works if "View controller-based status bar appearance" == YES
#implementation UIImagePickerController (IOS7_StatusBarStyle)
-(UIViewController*)childViewControllerForStatusBarStyle
{
return nil;
}
-(UIStatusBarStyle)preferredStatusBarStyle
{
return UIStatusBarStyleLightContent;
}
#end
All the answers above is ok and can help.
I had the same problem having to manage the application runned under different iOS versions.
UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
if(IS_IOS8_AND_UP) {
imagePickerController.modalPresentationStyle = UIModalPresentationFullScreen;
} else {
imagePickerController.modalPresentationStyle = UIModalPresentationCurrentContext;
}
imagePickerController.delegate = self;
[self presentViewController:imagePickerController animated:YES completion:nil];
Then, in delegate:
- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
/* Cancel button color */
_imagePicker.navigationBar.tintColor = <custom_color>
/* Status bar color */
[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault];
}
Yet another solution which may work in some of the situations.
let imagePicker = UIImagePickerController()
imagePicker.sourceType = .PhotoLibrary
imagePicker.navigationBar.barStyle = .Black
Have you tried calling [self setNeedsStatusBarAppearanceUpdate] when your presenting view controller reappears?
I try to hide the status bar in UIImagePickerController in iOS7, but I still don't know how to do this. I use
- (void)viewWillAppear:(BOOL)animated
{
[[UIApplication sharedApplication] setStatusBarHidden:YES
withAnimation:UIStatusBarAnimationNone];
}
in the ViewController that call the UIImagePickerController, and set "View controller-based status bar appearance = NO" in the plist file. Hope this can help.
try this :
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
and in the protocol implement, use this:
- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated
{
[[UIApplication sharedApplication] setStatusBarHidden:YES];
}
This solved it for me...:
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
[[UIApplication sharedApplication] setStatusBarHidden:YES];
[picker dismissViewControllerAnimated:YES completion:nil];
}
Nothing here specifically fixed the problem in that I was having (and perhaps that the OP was having too), so I thought I would share my answer. Instead of hiding the status bar which I think is a buggy solution (I noticed that it would sometimes leave my app in a state where the status bar was hidden when it shouldn't be). I instead opted to try and play nice with the UIStatusBarStyles.
When the UIImagePickerController has its view presented I set the status bar style to default since the default background color is a light grey.
- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated
{
[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault animated:YES];
}
Then, when the image picker is dismissed, I set it back to the UIStatusBarStyleLightContent.
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
//work
[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent animated:YES];
[self dismissViewControllerAnimated:YES completion:NULL];
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
//work
[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent animated:YES];
[self dismissViewControllerAnimated:YES completion:NULL];
}
In this case,We are using 2 steps
In first step:
Add in info.plist: "View controller-based status bar appearance" with value "NO"
In Second step: Use/call this code with delegate of UIImagePickerController
- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated
{
if([navigationController isKindOfClass:[UIImagePickerController class]])
[[UIApplication sharedApplication] setStatusBarHidden:YES];
}
In case of IOS-7 add One more Function
- (BOOL)prefersStatusBarHidden
{
return YES;
}
As of iOS 8.1, it seems like they've finally fixed this bug! I was able to remove all of the workarounds I employed from my code.
Using the default iOS 8 behaviour I was having problems with the status bar appearing when I wanted it hidden.
The solution I found was that, directly after calling presentPopover from my view controller I did:
[self performSelector:#selector(setNeedsStatusBarAppearanceUpdate) withObject:nil afterDelay:0.01];
I also had to add this to my main view controller:
- (UIViewController *)childViewControllerForStatusBarHidden
{
return nil;
}
So I had this problem and I was able to solve it by simply implementing a single delegate function. The background of my status bar is black, and so UIStatusBarStyle for my application is .LightContent. When I presented the UIImagePickerController to select a photo from the device storage, the status bar was fine. However, upon clicking into a directory such as "Camera Roll" or "Favorites," effectively pushing onto the navigation stack, the status bar disappeared. Upon selecting a photo, there was no status bar at all; upon dismissing another modal view controller, only the battery was present, indicating the rest of the status bar may be black as well.
I tried some of the other solutions such as extending UIImagePickerController, but in Swift, you cannot override using extensions. I then tried to subclass UIImagePickerController and tried to hide its status bar on viewWillAppear() and unhiding the status bar on viewWillDisappear. I was able to see the status bar hide with a .Slide animation, but since the status bar was invisible upon selecting a directory, I was not able to see the status bar unhide. Again, the green battery came back with the rest of the status bar invisible upon dismissing a modal view controller. I also tried overriding prefersStatusBarHidden(), but that function was never called, so I tried calling setNeedsStatusBarAppearanceUpdate() to ensure that prefersStatusBarHidden() is called by the system, but it still is not called. Also, there is the suggestion to set the status bar to be hidden on the delegate method navigationController willShowViewController. Once again, all this does is hide the status bar, which does not solve the problem. As it turns out, it seems that the status bar style is changed upon pushing onto the navigation stack of the UIImagePickerController. To solve the problem entirely, I did not have to write extensions or subclass UIImagePickerController. All you need to do is set the delegate and set the status bar style to remain the same. This addition made it as if the problem never existed.
let pickerController = UIImagePickerController()
pickerController.delegate = self
func navigationController(navigationController: UINavigationController, willShowViewController viewController: UIViewController, animated: Bool) {
UIApplication.sharedApplication().setStatusBarStyle(.LightContent, animated: false)
}
I actually found a better way to set the status bar background color in Image Picker. Basically you need to set the backgroundImage from the navigationBar to nil, because is default in Image Picker has a backgroundImage as a white Image.
Related
I'm having a lot of troubles with the new auto hiding of the status bar in iOS 8.
In my app, I've got a view in which when the user taps once, the navigation bar and the status bar disappear.
When in landscape, the status bar hides by itself and it's fine to me. I only need it in portrait mode.
But the problem is that when the device is landscape and the bar are shown, when the user taps twice to toggle the bar (so showing), and turns the device in portrait mode, the status bar is still hidden.
Basically I need to be able to hide the statusBar without interfere with its natural behavior on iOS 8, so I recap the scenario:
User enters said view with tabBar and NavigationBar and statusBar;
Taps once in the view and the bars disappear
User rotates the device, statusBar not appearing - OK, I want this
User taps again to show the bars - StatusBar still hidden, OK.
User rotates from landscape to portrait and..
statusBar still hidden - NOT OK.
MRW >
(source: mshcdn.com)
I've tried to adjust the statusBar on willRotate, but I made a mess in which the statusBar would be visible when it wasn't supposed to. Code I'm using:
- (BOOL)prefersStatusBarHidden
{
return statusBarHidden;
}
-(void)toggleBars:(UITapGestureRecognizer *)gesture{
CATransition *animation = [CATransition animation];
animation.type = kCATransitionFromBottom;
animation.subtype = kCATransitionFromTop;
animation.duration = .2f;
[animation setTimingFunction:[CAMediaTimingFunction functionWithName: kCAMediaTimingFunctionEaseInEaseOut]];
BOOL toggleNavigationBar = self.navigationController.navigationBarHidden;
[self.navigationController.navigationBar.layer addAnimation:animation forKey:nil];
[self.navigationController setNavigationBarHidden:!toggleNavigationBar animated:YES];
BOOL toggleTabHidden = self.tabBarController.tabBar.hidden;
if(![[[NSUserDefaults standardUserDefaults] objectForKey:#"showTabBar"]isKindOfClass:[NSNull class]]){
if([(NSNumber*)[[NSUserDefaults standardUserDefaults] objectForKey:#"showTabBar"]boolValue])
{
[self.tabBarController.tabBar.layer addAnimation:animation forKey:nil];
[self.tabBarController setHideTabBar:!toggleTabHidden animated:YES];
}
}
statusBarHidden = [UIApplication sharedApplication].statusBarHidden;
[[UIApplication sharedApplication] setStatusBarHidden:!statusBarHidden withAnimation:UIStatusBarAnimationSlide];
[self setNeedsStatusBarAppearanceUpdate];
if (IS_IOS8){
if (UIInterfaceOrientationIsLandscape(self.interfaceOrientation)){
if (statusBarHidden){
[[UIApplication sharedApplication] setStatusBarHidden:YES];
}
}
}
}
I was thinking about setting a flag in which when the statusBar is hidden when in landscape and all the controls are there, on rotation it would trigger the statusBar. With no success, apparently..
Any help highly appreciated.
Are you using UIViewController-based status bar appearance?
If you implement prefersStatusBarHidden I assume you are.
Now,
[[UIApplication sharedApplication] setStatusBarHidden:!statusBarHidden withAnimation: UIStatusBarAnimationSlide];
is not supposed to work with UIViewController-based status bar appearance.
You need just to return different value from prefersStatusBarHidden method and call setNeedsStatusBarAppearanceUpdate to notify the app that returning value has changed.
So to change statusbar visibility you should just do
#property (nonatomic, assign) BOOL hideStatusBar;
- (BOOL)prefersStatusBarHidden
{
return self.hideStatusBar;
}
- (void)toggleBars:(UITapGestureRecognizer *)gesture
{
... hide navbar and tabbar ...
self.hideStatusBar = ! self.hideStatusBar;
[self setNeedsStatusBarAppearanceUpdate];
}
And that's it!
This worked for me:
- (void)viewWillLayoutSubviews {
[[UIApplication sharedApplication] setStatusBarHidden:NO withAnimation:UIStatusBarAnimationNone];
}
This is the swift (3.0) version of that prefersStatusBarHidden
override var prefersStatusBarHidden: Bool{
return false
}
You just need to add it to your ViewController
#pragma mark After and Before Oriantation Change Methods+Delegate
- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
{
[[UIApplication sharedApplication] setStatusBarHidden:NO withAnimation:UIStatusBarAnimationNone];
}
#pragma mark nav
- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated
{
[[UIApplication sharedApplication] setStatusBarHidden:NO];
}
This is Short and Easy method.
I have some wierd bug with UINavigationBar.
Sometimes it just disappears (actually if you move view to the half of screen, and then just release it)
Video example
In the first ViewController's viewWillAppear: method i call:
[self.navigationController setNavigationBarHidden:NO animated:YES];
The second ViewController's viewWillAppear: contains:
[self.navigationController setNavigationBarHidden:YES animated:NO];
I tried change animated: parameter, but it doesn't help.
Is it iOS7 bug or I just doing something wrong?
I found the reason for this.
That's happened because in info.plist
View controller-based status bar appearance is equal to YES
If change it to NO, then all will be fine
I got the same problem, and fixed it. the solution is:
Modify info.plist, set "View controller-based status bar appearance " to NO;
Delete all - (UIStatusBarStyle)preferredStatusBarStyle {} ;
If your view controller has different status bar style, use [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
e.g. in viewWillAppear set to light, in disappear ,set to dark style.
You should define appearance per navigation controller.
If you want to have a navigation bar on the second controller only you should do the following in that particular controller:
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[self.navigationController setNavigationBarHidden:NO animated:YES];
}
- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
[self.navigationController setNavigationBarHidden:YES animated:YES];
}
That way it will event work if you would need to change order of your controllers.
In my project I need to attach another uiviewcontroller to the end of the imagepicker chain.
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)img editingInfo:(NSDictionary *)editInfo
{
uploadPhotoViewController * newView = [[uploadPhotoViewController alloc] initWithNibName:#"uploadPhotoViewController" bundle:nil];
newView.contest = received;
newView.receivedImage = img;
newView.parentImagePicker = picker; // this doesn't help
[picker pushViewController:newView animated:YES];
}
This works great, but when I use the camera as the source it clears the navigation bar and I can not get it to become visible. I have tried the usual:
-(void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
self.wantsFullScreenLayout = FALSE;
self.navigatioController.navigationBar.hidden = false;
self.navigationController.navigationBar.barStyle = UIBarStyleBlackTranslucent;
[[UIApplication sharedApplication] setStatusBarHidden:NO animated:NO];
[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleBlackTranslucent animated:YES];
}
The status bar will come back in the pushed view, but the navigation bar is another beast. I am guessing that since this is a modal that the navigationcontroller is being sent to the view stack underneath. Does anyone know how to address the image picker? I tried passing the image picker in as an object for the view controller as you can see in the first codeblock and then I tried addressing it like this:
self.parentImagePicker.navigationBar.hidden = false;
self.parentImagePicker.navigationBar.barStyle = UIBarStyleBlackTranslucent
But that didn't help either. So I'm stumped. Is their some ivar or something that will allow me to address the picker?
Oh cool, I figured it out. while the call
self.navigatioController.navigationBar.hidden = false;
does not work,
[self.navigationController setNavigationBarHidden:NO];
this one does.
Is there a way to change the tint/background color of UIDocumentInteractionController navigationbar?
A cleaner version of #DOOManics implementation:
- (UIViewController *)documentInteractionControllerViewControllerForPreview:(UIDocumentInteractionController *)controller
{
return [self navigationController];
}
If you put the UIDocumentInteractionController onto a UINavigationController it will automatically take the color its navbar. This is probably your root view navcontroller.
You do this with the documentInteractionControllerViewControllerForPreview method:
- (UIViewController *) documentInteractionControllerViewControllerForPreview: (UIDocumentInteractionController *) controller
{
// Use the rootViewController here so that the preview is pushed onto the navbar stack
MyAppDelegate *appDelegate = (MyAppDelegate *)[[UIApplication sharedApplication] delegate];
return appDelegate.window.rootViewController;
}
[[UINavigationBar appearance] setTintColor:[UIColor colorWithRed:107.0/256.0 green:145.0/256.0 blue:35.0/256.0 alpha:1.0]];
Place this code in Appdelegate's didFinisLaunching method. It will change the color of the navigation bar for the whole app.
Try this code:
- (void)openEC:(NSURL*)url {
[UINavigationBar appearance].tintColor = [UIColor blueColor];
docController = [UIDocumentInteractionController interactionControllerWithURL:url];
[docController setDelegate:self];
[docController presentOptionsMenuFromRect:self.view.bounds inView:self.view animated:YES];
}
- (void)documentInteractionControllerDidDismissOptionsMenu:(UIDocumentInteractionController *)controller {
[UINavigationBar appearance].tintColor = [UIColor whiteColor];
}
Swift version to #dvdfrddsgn implementation
Try this : (You need to implement UIDocumentInteractionControllerDelegate)
func documentInteractionControllerViewControllerForPreview(_ controller: UIDocumentInteractionController) -> UIViewController {
return self.navigationController ?? self
}
If you're not using a navigationController, you can set the navigation bar color in the UIDocumentInteractionController by setting the correct settings on the View of the UIViewController where you launch the UIDocumentInteractionController from.
Let's say you have UIViewController viewController1 (from somewhere here you launch the UIDocumentInteractionController), with a View1 in the storyboard.
With the Storyboard open, click on the View1 from the list of elements on the viewController1 and go to "Attributes inspectors" on the right side. The Background and the Tint set there will be used in your UIDocumentInteractionController as well afterwards.
Then you can just use:
- (UIViewController *)documentInteractionControllerViewControllerForPreview:(UIDocumentInteractionController *)controller
{
return self;
}
Note that inside the viewController1, you might have a Navigation Bar with different properties, and these will not be used in the UIDocumentInteractionController.
I have a strange issue on ios 4.3.I have one of my screen in landscape mode, a button click presents a popover.My popover has a search bar.Whenever keyboard appears it automatically pushes my popover bit up.When I resign the keyboard , popover reduces in height.This is the issue only on ios 4.3.While in rest of the ios , my popover doesnot reduces in height after keyboard dismissal.
None of the answers above worked for me. Apparently the keyboard scales the view and restores this scaling after the UIKeyboardDidHideNotification notification, making the presentPopoverFromRect method useless when applied handling this notification.
The way I solved it was by delaying the latter call as follows:
- (void)viewDidLoad
{
[super viewDidLoad];
popup = nil; //my ViewController with UITextField
popover = nil; //my UIPopoverController
NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
[nc addObserver:self
selector:#selector(resizePopup:)
name:UIKeyboardDidHideNotification
object:nil];
}
- (void)doDelayedResize
{
[popover presentPopoverFromRect:myButton.bounds inView:myButton permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
}
- (void)resizePopup:(NSNotification*)note
{
[self performSelector:#selector(doDelayedResize) withObject:nil afterDelay:0.01];
}
I answered a very similar question here: UIPopoverController's view controller gets resized after keyboard disappears
The way I got around it was to observe the keyboard disappearing in the controller which controls the UIPopoverController:
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(presentSearchPopover) name:UIKeyboardDidHideNotification object:nil];
And then in -presentSearchPopover, present the UIPopoverController again (it's quite a seamless transition):
- (void)presentSearchPopover
{
self.searchPopoverController.popoverContentSize = CGSizeMake(width, height));
[self.searchPopoverController presentPopoverFromRect:someRect) inView:self.view permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES];
}
Don't forget to remove the observer in -dealloc or similar too:
- (void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardDidHideNotification object:nil];
[super dealloc];
}
I found an answer for this.It was a bug with the top arrow of popover.If I use the left arrow direction for popover, everything works fine.
I ran into this issue as well - specifically, the popover wasn't growing back to its pre-keyboard size after tapping away from the popover. (The popover would grow back if the user dismissed the keyboard directly or the popover's view controller resigned first responder).
Unfortunately, I have to use the top arrow direction for the popover due to the UI's layout. To solve this, the view controller responsible for the popover implements - (void)popoverControllerDidDismissPopover:(UIPopoverController *)popoverController. For example:
#interface MyController : UIViewController <UIPopoverControllerDelegate>
{
// ...
}
//...
#end
Set that controller as the popover's delegate:
MyPopoverViewController *popoverVC = [[MyPopoverViewController alloc] init];
UIPopoverController *myPopover = [[UIPopoverController alloc] initWithContentViewController:popoverVC];
myPopover.delegate = self;
// Hang on to popoverVC, myPopover or release them as desired...
In addition, my popover's view controller sets its contentSizeForViewInPopover property to the desired size:
#implementation MyPopoverViewController
- (id)init
{
self = [super init];
if (self)
{
// ...
self.contentSizeForViewInPopover = CGSizeMake(320, 400); // desired size
}
return self;
}
When the keyboard causes the popover to shrink, it affects the popover's popoverContentSize and not its view controller's contentSizeForViewInPopover. Therefore, reset popoverContentSize in MyController's delegate method:
- (void)popoverControllerDidDismissPopover:(UIPopoverController *)popoverController
{
// Check if popoverController is valid, the popover you want, etc
popoverController.popoverContentSize = popoverController.contentViewController.contentSizeForViewInPopover;
}
Here is my solution:
1. Register for keyboard Notifications (UIKeyboardWillShowNotification, UIKeyboardWillHideNotification)
2. Create local variables:
CGSize _currentPopoverContentSize; //if you want to have custom size for popover
UIView *_currentPopoverSender; //to remember from wich view you will present popover
BOOL _keyboardIsShown; //enable in keyboardWillShow, and disable in keyboardWillHide
3. In my presentPopover method:
- (void)presentPopoverControllerWithSize:(CGSize)size fromView:(UIView *)sender{
MyController *controller = [[[MyController alloc] init] autorelease];
if (self.popover)
{
[_popover release];
_popover = nil;
}
_popover = [[UIPopoverController alloc] initWithContentViewController:controller];
_popover.popoverContentSize = size;
_popover.delegate = self;
//checking if keyboard is shown - if NO, than present popover, if YES - just `resignFirstResponder` for your _`activeTextField`(you can set it in -textFieldDidBeginEditing: and nullify in -textFieldDidEndEditing:)
if (!_keyboardIsShown)
{
[_popover presentPopoverFromRect:[sender bounds]
inView:sender
permittedArrowDirections:UIPopoverArrowDirectionUp
animated:YES];
}
else
{
[_activeTextField resignFirstResponder];
}
_currentPopoverContentSize = size;
_currentPopoverSender = sender;
}
4. Than:
- (void)keyboardWillBeHidden:(NSNotification*)aNotification{
[UIView animateWithDuration:0.3
animations:^{
//do some stuff
[self.scrollView setContentSize:_scrollViewContentSize];
} completion:^(BOOL finished) {
if (_popover && _currentPopoverSender)
{
[_popover presentPopoverFromRect:[_currentPopoverSender bounds]
inView:_currentPopoverSender
permittedArrowDirections:UIPopoverArrowDirectionUp
animated:YES];
}
}];
_keyboardIsShown = NO;
}
Hi After going through the forum, I don't think it's a bug after playing with frame sizes a lot, working on IOS 4,5,6,7 it's the same behaviour.
The solution for me was to:
1) Go into the designer by
2) Opening the XIB ViewController that is causing the problem (i.e. the PopOver one).
3) Click to select it's VIEW.
4) Uncheck "AutoResizeSubviews"
5) When loading the PopOver in code, make sure you do:
6) Your_Popup_Window.popoverContentSize = Your_ViewController.view.bounds.size;
I hope this helps.
Kind Regards
Heider Sati