xcode: ios8, rotate on single view of the app - ios

I have an app that I have set to only run on portrait mode only.
But I have a page view where I am showing a photo but wants to enable it to auto rotate with the device. I tried to put these codes but its not working
-(NSUInteger)supportedInterfaceOrientations {
return UIInterfaceOrientationMaskAll;
}
-(BOOL)shouldAutorotate {
return YES;
}
Can someone please advise what's needed to do now?

You have to set the point of rotation on your imageview
UIImageView *yourImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"logo.png"]];
yourImageView.center = CGPointMake(100.0, 100.0);
yourImageView.transform = CGAffineTransformMakeRotation(M_PI_2);

Related

iOS : How to Full screen video when change device orientation to Landscape in MPMoviePlayerController?

I am developing an application with MPMoviePlayerController. The application supports Portrait mode only. But I want to change the video in full Screen when I change the device orientation to landscape and back to half screen when change device orientation to Portrait.
if in Landscape and Full Screen mode and movie finishes then also go to half screen mode.
I have tried different codes and options but could not succeed. please help.
My Source code
#property (nonatomic,strong) MPMoviePlayerController* moviePlayer;
-(void)PlayVideoContent
{
CGFloat x = 0;
CGFloat y = 70;
CGRect mpFrame = CGRectMake(x, y, SCREEN_WIDTH, 200);
NSString * introVideoFileName = #"video_5.mp4";
NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:introVideoFileName ofType:#""]];
MPMoviePlayerController *controller = [[MPMoviePlayerController alloc] initWithContentURL:url];
controller.scalingMode = MPMovieScalingModeAspectFill;
self.moviePlayer = controller; //Super important
// controller.view.frame = self.view.bounds; //Set the size
controller.view.frame = mpFrame; //Set the size
// [self.moviePlayer setFullscreen:YES animated:YES];
[self.view addSubview:self.moviePlayer.view]; //Show the view
[self.moviePlayer play]; //Start playing
}
U must give the UIViewController(s) of your application to decide whether it's in landscape or portrait.
After that, set all the rest to portrait except the one u want in landscape (the MPMoviePlayerController)
In your project settings (App Target > General > Deployment Info > Device Orientation), select Portrait, Landscape Left and Landscape Right.
In your root view controller, add:
- (BOOL)shouldAutorotate
{
return YES;
}
- (NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskPortrait;
}
If everything gets loaded into this view controller, that should be all you have to do. If you find that some views are rotating when they shouldn't, add this same code to their view controllers.
The controller for the fullscreen video will use the supported orientations specified in the target settings, and so will allow rotation to landscape. When you close the video, the view will rotate back to portrait.

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...)

UIImageView rotating but not properly repositioning

I am building an app that displays an image in landscape and portrait modes. Rotating works perfectly. The image is also perfectly positioned in landscape mode. However it keeps its landscape coordinates in portrait, which misplace it as a result. Please find my code below. Could you let me know what I'm missing? Is there also a way to achieve this strictly from a Xib file?
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
[[self navigationController] setNavigationBarHidden:YES animated:NO];
UIImage *startImage = [UIImage imageNamed:#"title.png"];
UIImageView *startImageView = [[UIImageView alloc] initWithImage:startImage];
if (curOrientation == UIInterfaceOrientationPortrait || curOrientation == UIInterfaceOrientationPortraitUpsideDown) {
[startImageView setFrame:CGRectMake(-128, 0, 1024, 1024)];
}else{
[startImageView setFrame:CGRectMake(0, -128, 1024, 1024)];
}
[self.view addSubview:startImageView];
}
Currently you are only calling this code when the view is first loaded. You actually need to call it
whenever the view appears onscreen (in case the device was rotated while it was offscreen)
whenever the device is rotated
but you should keep the view creation code in viewDidLoad, as you only want to create it once.
Make a property to keep a pointer to the view so that you can refer to it from all of these places in your codeā€¦
#property (nonatomic, weak) UIImageView* startImageView;
Create it in viewDidLoad (but don't worry then about the geometry, as you can do this in viewWillAppear):
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
[[self navigationController] setNavigationBarHidden:YES animated:NO];
UIImage *startImage = [UIImage imageNamed:#"title.png"];
UIImageView *startImageView = [[UIImageView alloc] initWithImage:startImage];
self.startImageView = startImageView;
[self.view addSubview:startImageView];
}
Make a generic orientation method:
- (void) orientStartImageView
{
UIInterfaceOrientation curOrientation = [UIApplication sharedApplication].statusBarOrientation;
if (curOrientation == UIInterfaceOrientationPortrait || curOrientation == UIInterfaceOrientationPortraitUpsideDown) {
[self.startImageView setFrame:CGRectMake(-128, 0, 1200, 1200)];
}else{
[self.startImageView setFrame:CGRectMake(0, -128, 1200, 1200)];
}
}
Call it from viewWillAppear (triggered every time the view comes onscreen):
- (void) viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[self orientStartImageView];
}
Call it from viewWillLayoutSubviews (triggered every time the view IS onscreen and the device rotates):
- (void)viewWillLayoutSubviews
{
[super viewWillLayoutSubviews];
[self orientStartImageView];
}
By the way, I am not sure your frames are correct - in portrait you are shifting the left edge offscreen, in landscape you are shifting the top edge offscreen. Is that what you want? It may well be that you can achieve what you want in Interface Builder, but it is not clear from your code what that is - maybe you could post a picture. Also check that you have Autolayout disabled (checkbox in Interface Builder's file inspector) to simplify issues.
update
You may be able to do this from the Xib with no code: centre the imageView in it's superView, set it's size to your final size (eg 1200x1200), disable Autolayout, deselect all springs and struts, set your View Mode appropriately (eg center or scaleToFill)

Why is my ModalView displaying in portrait mode?

Hopefully somebody can point out what I'm doing wrong with my Splash screen here. The problem is that the screen is displaying in portrait mode, not landscape...
- (void)showSplash
{
UIView *modelView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 1024 , 748)];
UIViewController *modalViewController = [[UIViewController alloc] init];
[modelView setBackgroundColor:[[UIColor alloc] initWithPatternImage:[UIImage imageNamed:#"Splash.png"]]];
modalViewController.view = modelView;
[self presentModalViewController:modalViewController animated:NO];
[self performSelector:#selector(hideSplash) withObject:nil afterDelay:5.0];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationLandscapeLeft || interfaceOrientation == UIInterfaceOrientationLandscapeRight);
}
Thanks in advance for the help.
Looks like you are writing app for iPad. If so, you have to support both landscape as well as portrait orientation otherwise Apple will reject it. I would suggest that you should use two different images. Image specifications are as follows:
Default-Landscape.png (1004 * 768)
Default-Portrait.png (748*1024)
(I am assuming that you are showing status bar if not add 20 pixels to height of an image)
That's it, create these images and add it to your project. And you are good to go. No Need to write any additional piece of code too..
And ya make it
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return YES;
}
You shouldn't rely on a function in your code to display the splash screen. Just define them as the previous answer from Sumit Lonkar explains.
If you do it in code, I believe at the start of the application the orientation is always considered as portrait, then the transition to the actual orientation is triggered. This explains why your code displays first as portrait and most likely there is nothing else in the code to handle rotation. Besides, the purpose of the splash screen is to display something while the app is loading, so if you put it in code you lose the purpose.
By doing it the Apple way you leave it to another Apple process that runs before looking at your code and it will work.
Regarding the orientation supported I have on my iPad some apps that support only landscape (TapZoo for example) so it should be ok with Apple.

Moving objects around when the view rotates

I have an iPad app that I would like to work in the sideways orientation instead of just portrait. I have programatically placed images, labels, and buttons into my view and used CGRectMake (x,x,x,x) to tell them where to go on the view into the center. When the app rotates horizontally, I need my labels and buttons to shift up (since they can't go down as far when in landscape mode), but stay in the center. Here is some code I've been playing with:
if((self.interfaceOrientation == UIDeviceOrientationLandscapeLeft) || (self.interfaceOrientation == UIDeviceOrientationLandscapeRight))
{
lblDate = [[UILabel alloc] initWithFrame:CGRectMake(384-(fieldWidth/2)-30,controlTop+45,120,40)]; //these dimensions aren't correct, though they don't matter here
lblDate.text = #"Date:";
lblDate.backgroundColor = [UIColor clearColor];
[contentView addSubview:lblDate];
} else {
//the orientation must be portrait or portrait upside down, so put duplicate the above code and change the pixel dimensions
}
Thanks for your help!
Take a look at this: iphone/ipad orientation handling
You just specify each control location depending on the rotation.
I know this might be a bit of an old question now looking to the date, but I just very recently faced the same problem. You could stumble upon many suggestions such as transforming main view's subviews or it's layers. Non of this worked for me.
Actually the solitary solution I've found is that since you want your UI controls to be located dynamically then don't deploy them mainly in the interface builder. The interface builder can be helpful knowing the desired locations for dynamic controls in both portrait and landscape orientations. i.e make two separate test views in the interface builder, one portrait and the other landscape, align your controls as you wish and right down X, Y, Width and Height data just to use with CGRectMake for each control.
As soon as you write down all needed positioning data from the interface builder get rid of those already drawn controls and outlets/actions links. They will be of no need now.
Of course don't forget to implement UIViewController's willRotateToInterfaceOrientation to set control's frame with each orientation change.
#interface
//Declare your UI control as a property of class.
#property (strong, nonatomic) UITableView *myTable;
#end
#implementation
// Synthesise it
#synthesize myTable
- (void)viewDidLoad
{
[super viewDidLoad];
// Check to init for current orientation, don't use [UIDevice currentDevice].orientation
if (self.interfaceOrientation == UIInterfaceOrientationLandscapeLeft || self.interfaceOrientation == UIInterfaceOrientationLandscapeRight)
{
myTable = [[UITableView alloc] initWithFrame:CGRectMake(20, 20, 228, 312)];
}
else if (self.interfaceOrientation == UIInterfaceOrientationPortrait)
{
myTable = [[UITableView alloc] initWithFrame:CGRectMake(78, 801, 307, 183)];
}
}
myTable.delegate = self;
myTable.dataSource = self;
[self.view addSubview:myTable];
}
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
if (toInterfaceOrientation == UIInterfaceOrientationLandscapeRight || toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft)
{
// Show landscape
myTable.frame = CGRectMake(20, 20, 228, 312);
}
else
{
// Show portrait
myTable.frame = CGRectMake(78, 801, 307, 183);
}
}

Resources