简体   繁体   中英

Using UIImagePickerController in iPad Mini iOS 7

I have an app targeting iPhone. The UIImagePickerController works fine on iPhone, but when I open it with iPad Mini on iOS 7, the top part of UIImagePickerController was hidden, which hide the front/back camera toggle button. How can I solve this?

Update: I observed through subview hierarchies that the "CAMFlipButton" has wrong frame:

<CAMFlipButton: 0x176e6250; baseClass = UIButton; frame = (310.5 9.5; 48 70); opaque = NO; layer = <CALayer: 0x176e63c0>>

它显示如下

I had the same issue; it seems to affect only the iPad Mini (but only the non-retina version), on both iOS 7 and 8. Not sure why not many people faced this issue, but I couldn't find a working solution or workaround.
So what I did (what I hacked!) is I detect when this happens (when the button ends up outside the window bounds), and correct it, by moving the button back into the window, and adding my own image to the button.

@interface MyImagePickerController : UIImagePickerController
@end

@implementation MyImagePickerController

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    showFlipButtonInSubviews(self.view);
}

void showFlipButtonInSubviews(UIView *view) {
    if ([[[view class] description] isEqualToString:@"CAMFlipButton"]) {
        if (view.x + view.width > UIScreen.mainScreen.bounds.size.width + 5) {
            // Fixes this: http://stackoverflow.com/questions/20895993/using-uiimagepickercontroller-in-ipad-mini-ios-7
            // Happens on iPad Mini non-retina only
            view.x = UIScreen.mainScreen.bounds.size.width - view.width - 10;
            UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 10, 23, 16)];
            imageView.image = [UIImage imageNamed:@"switch-camera"];
            [view addSubview:imageView];
        }
    } else {
        for (UIView *subview in [view subviews]) {
            showFlipButtonInSubviews(subview);
        }
    }
}

@end

Why UIScreen.mainScreen.bounds.size.width + 5 you ask? Simply because on the iPad Mini retina, that button has 4 pixels outside the window, but it still shows correctly, so I don't want to apply this hack then.

My switch-camera image looks like this:
开关相机
(hard to see, it's white! right-click or drag it around to see it...)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM