Why is the top of the scene on the right side of device's camera viewfinder for iPad Landscape only? - uiimagepickercontroller

UIImagePickerController, UIPopoverController,iPad
Inside my app, the photo view and the photos taken are as expected in Portrait mode, i.e. HomeButton
at the bottom. But with the iPad only, (iPhone is fine) if I use Landscape, and point the iPad camera at a ladder leaned up against the house, say from the ground to the eaves, the eaves are running vertically along the right side of the popOver camera view.
That is, the popover camera viewer has rotated into the correct position (long side of viewer are parallel to the long sides of the device, but the image displayed is rotated 90 degrees right.
How do I fix this problem?
Thanks for reading, Mark
- (IBAction)grabPhotoAction
{
UIImagePickerController* imagePicker = [[UIImagePickerController alloc] init];
imagePicker.delegate = self;
imagePicker.sourceType = (cameraSelected) ?
UIImagePickerControllerSourceTypeCamera : UIImagePickerControllerSourceTypePhotoLibrary;
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
{
self.myPopover = [[classPopoverController alloc] initWithContentViewController:imagePicker];
[myPopover presentPopoverFromRect:CGRectMake(0.0, 0.0, 400.0, 400.0)
inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
}else {
[self presentViewController:imagePicker animated:NO completion:NULL];
}
}

I found the answer online just before posting this question. I decided it might well be helpful to others. The answer is quite simple. Forget all about using the special treatment for iPad and simply display full screen for both iPhone and iPad.
- (IBAction)grabPhotoAction
{
UIImagePickerController* imagePicker = [[UIImagePickerController alloc] init];
imagePicker.delegate = self;
imagePicker.sourceType = (_cameraSelected) ?
UIImagePickerControllerSourceTypeCamera : UIImagePickerControllerSourceTypePhotoLibrary;
[self presentViewController:imagePicker animated:NO completion:NULL];
}

Related

UIView not resizing when rotated with a CGAffineTransform under iOS8

I have a UIViewController that only rotates some of it subviews when the device is rotated. This works fine under iOS7 but breaks under iOS8. It appears that the UIView's bounds are adjusted by the transform under iOS8. This was unexpected.
Here's some code:
#interface VVViewController ()
#property (weak, nonatomic) IBOutlet UIView *pinnedControls;
#property (nonatomic, strong) NSMutableArray *pinnedViews;
#end
#implementation VVViewController
- (void)viewDidLoad
{
[super viewDidLoad];
self.pinnedViews = [NSMutableArray array];
[self.pinnedViews addObject:self.pinnedControls];
}
-(void)viewWillLayoutSubviews
{
[super viewWillLayoutSubviews];
[UIViewController rotatePinnedViews:self.pinnedViews forOrientation:self.interfaceOrientation];
}
- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
[super willAnimateRotationToInterfaceOrientation:toInterfaceOrientation duration:duration];
if (UIInterfaceOrientationIsLandscape(toInterfaceOrientation) && UIInterfaceOrientationIsLandscape(self.interfaceOrientation)) {
[UIViewController rotatePinnedViews:self.pinnedViews forOrientation:toInterfaceOrientation];
}
}
#end
We've made a category on UIViewController to handle this behavior. Here's the pertinent code:
#implementation UIViewController (VVSupport)
+ (void)rotatePinnedViews:(NSArray *)views forOrientation:(UIInterfaceOrientation)orientation {
const CGAffineTransform t1 = [UIViewController pinnedViewTansformForOrientation:orientation counter:YES];
const CGAffineTransform t2 = [UIViewController pinnedViewTansformForOrientation:orientation counter:NO];
[views enumerateObjectsUsingBlock:^(UIView *view, NSUInteger idx, BOOL *stop) {
// Rotate the view controller
view.transform = t1;
[view.subviews enumerateObjectsUsingBlock:^(UIView *counterView, NSUInteger idx, BOOL *stop) {
// Counter-rotate the controlsUIin the view controller
counterView.transform = t2;
}];
}];
}
+ (CGAffineTransform)pinnedViewTansformForOrientation:(UIInterfaceOrientation)orientation counter:(BOOL)counter {
CGAffineTransform t;
switch ( orientation ) {
case UIInterfaceOrientationPortrait:
case UIInterfaceOrientationPortraitUpsideDown:
t = CGAffineTransformIdentity;
break;
case UIInterfaceOrientationLandscapeLeft:
t = CGAffineTransformMakeRotation(counter ? M_PI_2 : -M_PI_2);
break;
case UIInterfaceOrientationLandscapeRight:
t = CGAffineTransformMakeRotation(counter ? -M_PI_2 : M_PI_2);
break;
}
return t;
}
#end
Here's what the nib looks like:
The UIView named pinned in the nib is the IBOutlet of pinnedControls:
When I run this in portrait mode under iOS7 or iOS8 I get this:
And I see the desired outcome under iOS7 in landscape mode:
But under iOS8 (GM) I do not get this behavior. This is what I see instead:
Notice that the center of the UILabel with the text "Pinned Label" is maintaining its distance from the bottom of the pinned UIView, which has not changed size to accommodate the rotation. That UIView has all its edges pinned to the top, left, bottom and right sides of the super view.
It looks to me that the transform property interacts with Auto Layout differently under iOS8. I'm a bit baffled here. I know I can't rely on the frame. I may just start manually setting bounds but that just seems like the wrong thing to do, essentially do an end run around Auto Layout.
So this was driving me crazy for the past couple days and I was able to fix by changing the timing of the setTransform call in my animations block
When going to landscape, I'm setting the transform AFTER setting up the new frame. When going portrait, I'm setting the transform BEFORE setting up the new frame. All this was going inside the animations block on the "animateWithDuration..." method
I'm not sure if it will help you directly with your code, but it might spark some inspiration to solve it since we are definitely having a similar issue
This is more of a work around than a fix so it may not help everybody in similar situations. My problem was that the outer "pinned" view was being resized again after the transform was applied.
My solution was to change the constraints on the pinned view to be center vertically, center horizontally, and width and height equal a constant.
Then, in viewDidLoad, I set the height and width of the pinned view's frame to be the height of the main screen. This makes the view square so I don't care if it gets an extra rotate.
in ios8, uiviewcontrollers need to be resized with uitraitcollections depending on the device orientation. Otherwise, you get a uiview in portrait mode, while the phone oriented in landscape, when you try to rotate it. So the correct steps are to rotate AND override uitraitcollections
EDIT:
I override my uitraitcollection with the following code
UITraitCollection *horTrait = [UITraitCollection traitCollectionWithHorizontalSizeClass:UIUserInterfaceSizeClassCompact];
UITraitCollection *verTrait = [UITraitCollection traitCollectionWithVerticalSizeClass:UIUserInterfaceSizeClassCompact];
UITraitCollection *finalTrait = [UITraitCollection traitCollectionWithTraitsFromCollections:#[horTrait,verTrait]];
[self.parentViewController setOverrideTraitCollection:finalTrait forChildViewController:self];
unfortunately, it doesnt work if the uiviewcontroller im trying to modify does NOT have a parentviewcontroller :'(

How is this navigation control created

The second image on this page from Apple's user interface design guide show a segmented control inside of a tall navigation bar:
https://developer.apple.com/library/ios/documentation/userexperience/conceptual/mobilehig/Anatomy.html#//apple_ref/doc/uid/TP40006556-CH24-SW1
How has this been done? It seems to me that a UINavigationBar is always 64 pixels high, so I don't understand how they made this taller.
Is it a custom element (which would be surprising in this document), or is there an easy way to achieve this? I'm wondering if it's a UIToolbar... are they merged with the UINavigationBar under iOS 7? If so, how do we do this?
Note that I need to do this in a iPad app, where the UINavigationController is inside a split view controller.
I finally found the solution to this.
I had to override UINavigation bar with my custom subclass in order to change the height. By using the appearance proxy the title and navigation items can be repositioned correctly. Unfortunately the proxy can't be used to shift the back button's arrow up (on iOS 7), so we have to override layoutSubview to handle that.
#define kAppNavBarHeight 66.0
#implementation TATallNavigationBar
- (id)initWithCoder:(NSCoder *)aDecoder {
self = [super initWithCoder:aDecoder];
if (self) {
[self setupAppearance];
}
return self;
}
- (id)init
{
self = [super init];
if (self) {
[self setupAppearance];
}
return self;
}
- (void)setupAppearance {
static BOOL appearanceInitialised = NO;
if (!appearanceInitialised) {
// Update the appearance of this bar to shift the icons back up to their normal position
CGFloat offset = 44 - kAppNavBarHeight;
[[TATallNavigationBar appearance] setTitleVerticalPositionAdjustment:offset forBarMetrics:UIBarMetricsDefault];
[[UIBarButtonItem appearanceWhenContainedIn:[RRSNavigationBar class], nil] setBackgroundVerticalPositionAdjustment:offset forBarMetrics:UIBarMetricsDefault];
[[UIBarButtonItem appearanceWhenContainedIn:[RRSNavigationBar class], nil] setBackButtonBackgroundVerticalPositionAdjustment:offset forBarMetrics:UIBarMetricsDefault];
[[UIBarButtonItem appearanceWhenContainedIn:[RRSNavigationBar class], nil] setBackButtonTitlePositionAdjustment:UIOffsetMake(0, offset) forBarMetrics:UIBarMetricsDefault];
appearanceInitialised = YES;
}
}
- (CGSize)sizeThatFits:(CGSize)size {
return CGSizeMake(self.superview.frame.size.width, kNavBarheight);
}
- (void)layoutSubviews {
static CGFloat yPosForArrow = -1;
[super layoutSubviews];
// There's no official way to reposition the back button's arrow under iOS 7. It doesn't shift with the title.
// We have to reposition it here instead.
for (UIView *view in self.subviews) {
// The arrow is a class of type _UINavigationBarBackIndicatorView. We're not calling any private methods, so I think
// this is fine for the AppStore...
if ([NSStringFromClass([view class]) isEqualToString:#"_UINavigationBarBackIndicatorView"]) {
CGRect frame = view.frame;
if (yPosForArrow < 0) {
// On the first layout we work out what the actual position should be by applying our offset to the default position.
yPosForArrow = frame.origin.y + (44 - kAppNavBarHeight);
}
// Update the frame.
frame.origin.y = yPosForArrow;
view.frame = frame;
}
}
}
#end
Note that it's easy to specify your subclass in XCode: clicking on the UINavigationController gives you access to the UINavigationBar in the left hand column. Click that and change it's subclass in the inspector.
I've also created a Gist for this:
https://gist.github.com/timothyarmes/7080170

After Taking Picture cannot select Use Photo or Retake

So I'm trying to update an app for iOS 7 and I'm running into issues with my custom overlay. The overlay is an image that I'm framing the photo with (both live and using a full resolution version to frame the final result in the camera roll). The problem is that now, under iOS 7, the overlay, while transparent at the bottom, provides access to the regular "take picture" button, but for some reason will not let me tap on the "Use Photo" or "Retake" buttons that come up after the picture is snapped. Here's the code snippet calling the view controller:
- (IBAction)takePhoto:(UIButton *)sender {
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.allowsEditing = NO;
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
picker.showsCameraControls = YES;
// Overlay Creation
UIView* overlayView = [[UIView alloc] initWithFrame:picker.view.frame];
overlayView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"PBOverlayView.png"]];
[overlayView.layer setOpaque:NO];
overlayView.opaque = NO;
picker.cameraOverlayView = overlayView;
[self presentViewController:picker animated:YES completion:NULL];
}
Another approach could be to observe the notifications when the ImagePicker changes state and remove (or disable) your overlay when you move into the "Use Photo" screen.
- (void) addPhotoObservers {
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(removeCameraOverlay) name:#"_UIImagePickerControllerUserDidCaptureItem" object:nil ];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(addCameraOverlay) name:#"_UIImagePickerControllerUserDidRejectItem" object:nil ];
}
- (void) removePhotoObservers {
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
-(void)addCameraOverlay {
if (self.cameraPicker) {
self.cameraPicker.cameraOverlayView = self.myCameraOverlayView;
}
}
-(void)removeCameraOverlay {
if (self.cameraPicker) {
self.cameraPicker.cameraOverlayView = nil;
}
}
You could set User Enteraction Enabled to NO on Overlay View ;) works for me
Your problem: When you initialised the overlayView, you've set the frame to be the same size as that of the picker.
UIView* overlayView = [[UIView alloc] initWithFrame:picker.view.frame];
Explanation: Before capturing an image, the camera buttons are in the foreground of the overlayView, so that there's no issue pressing them.
After capturing an image (on the "retake/use preview page"), the overlayView is the one to be in the foreground, thus blocking the access to the buttons.
I know this to be an issue on iOS7, and not sure about other versions.
Possible solutions: Since this is a native problem within Apple's UIImagePickerController, I can only think of two solutions: (1) If possible, configure the overlayView to have a shorter frame which doesn't cover the bottom of the picker; (2) If you need the overlay to cover these buttons, you still have the exhausting option of configuring self.imagePicker.showsCameraControls = NO; but then you'd have to customize ALL of the camera behaviour (you can find many examples for that on the web).
After taking a photo, or animation on your overlay is done, i made it work by removing the overlay from its superview.
[UIView animateWithDuration:1
delay:1
options:UIViewAnimationOptionCurveEaseOut
animations:^{
} completion:^(BOOL finished) {
[self.view removeFromSuperview];
}];

iOS7 UIImagePickerController cancel button disappear

The Cancel button is miss?! How can I fix this? Thank you very much.
if ([UIImagePickerController isSourceTypeAvailable: UIImagePickerControllerSourceTypePhotoLibrary])
{
if(buttonIndex == 1)
{
self.ctr = [[UIImagePickerController alloc] init];
self.ctr.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
self.ctr.delegate = self;
self.ctr.allowsEditing = YES;
[self presentModalViewController:self.ctr animated:YES];
}
}
Just change the UIImagePickerController navigationBar.tintColor, it should be OK.
self.ctr.navigationBar.tintColor = [UIColor redColor];//Cancel button text color
[self.ctr.navigationBar setTitleTextAttributes:#{UITextAttributeTextColor: [UIColor blackColor]}];// title color
Looks like apple made some mistake with it (iOS 10, Xcode 8) because just changing tint color of UIImagePickerController could not be done, cause, before controller isn't have topItem property, or navigationController property. So have done the changes in UIImagePickerController extension. But I checked navigationController and topItem in those overrided methods: viewDidLoad, viewWillAppear, viewDidAppear. but it still was nil. So i decide to check it in viewWillLayoutSubviews, and voila! It's wasn't nil, so we can set bar tint color of exact rightBarButtomItem here!
Here is example:
extension UIImagePickerController {
open override func viewWillLayoutSubviews() {
super.viewWillLayoutSubviews()
self.navigationBar.topItem?.rightBarButtonItem?.tintColor = UIColor.black
self.navigationBar.topItem?.rightBarButtonItem?.isEnabled = true
}
}
And don't forget to call super.viewWillLayoutSubviews, it's very important ;-)
EDIT: But it still has problems when return to the albums screen..
Change the tintColor
self.navigationBar.topItem?.rightBarButtonItem?.tintColor = UIColor.black
If that doesn't work run through your view controllers to see if there isn't a place where you changed the appearance of the navigation bar and reset the change.

UINavigationItem multiple line prompt text

Can any body give me the solution for displaying UINavigationItem prompt text in 2 lines?
There is not a built-in way to do this. Below is a work-around that seems to work pretty well that I put together from stackOverflow post UINavigationItem with prompt and activity indicator
Here is a simulator screen shot of what it creates:
Note that since the text is a UILabel you can modify its color, font, or anything else too.
// I have this code in viewDidLoad
UIView *viewContainingPrompt;
UIBarButtonItem *promptButtonItem;
// Configuring the prompt title of the navigation bar so it is present but empty
[self.navigationItem setPrompt: #""];
// We will create a UIBarButtonItem that has a custom view (viewContainingPrompt).
// A subview of viewContainingPrompt will be a UILabel (headerLabel)
// We need to have this "intermediate" view to position the label at the right position
// (the UIBarButtonItem ignores the origin and height of its custom view)
viewContainingPrompt = [[UIView alloc] initWithFrame: CGRectMake(0, 0, 0, 85)];
viewContainingPrompt.autoresizingMask = UIViewAutoresizingFlexibleWidth;
// Choose a width that puts 10 points on either end...
CGFloat labelWidth = self.navigationController.navigationBar.bounds.size.width - 20.0;
// Note that the '-60' below is determined by the width of the back button
// If someone can figure out how to determine this width at runtime this code
// would be much more robust.
UILabel *headerLabel = [[UILabel alloc] initWithFrame: CGRectMake(-60,-8,labelWidth,36)];
headerLabel.autoresizingMask = UIViewAutoresizingFlexibleWidth;
headerLabel.text = #"A quite long prompt string that will wrap to a second line to demonstrate multiline prompt.";
headerLabel.font = [UIFont systemFontOfSize: 14];
headerLabel.numberOfLines = 0; // Zero gives as many lines as will fit, could be 2
headerLabel.backgroundColor = [UIColor clearColor];
headerLabel.textColor = [UIColor colorWithRed: .1 green: .1 blue: .2 alpha: 0.8f];
headerLabel.shadowColor = [UIColor colorWithRed: 1 green: 1 blue: 1 alpha: 0.5f];
headerLabel.shadowOffset = CGSizeMake( 0, 1 );
headerLabel.textAlignment = UITextAlignmentCenter;
[viewContainingPrompt addSubview: headerLabel];
//[headerLabel release]; // Uncomment if not using ARC
promptButtonItem = [[UIBarButtonItem alloc] initWithCustomView: viewContainingPrompt];
self.navigationItem.leftBarButtonItem = promptButtonItem;
self.navigationItem.leftItemsSupplementBackButton = YES;
//[viewContainingPrompt release]; // Uncomment if not using ARC
//[promptButtonItem release]; // Uncomment if not using ARC
I would appreciate anyone's feedback on how to figure out the width of the back button during execution so that width did not have to be hard coded.
As it is I do not think there are any private APIs or other illegal code contained.

Resources