Landscape views only loading properly from portrait and after rotating, why? - uiimageview

new iOS developer here. I have multiple views that require different images to be displayed in portrait and landscape. I currently have implemented that successfully and the portrait image loads fine, and, upon rotation, the landscape image also loads fine. However, if the device is in landscape orientation then switches to another view, it loads improperly - wrong size, resolution, alignments, etc. My code for dealing with orientation changes is below:
- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
{
if((self.interfaceOrientation == UIDeviceOrientationLandscapeLeft) || (self.interfaceOrientation == UIDeviceOrientationLandscapeRight))
{
_image1.image = [UIImage imageNamed:#"Landscape.png"];
}
else if((self.interfaceOrientation == UIDeviceOrientationPortrait) || (self.interfaceOrientation == UIDeviceOrientationPortraitUpsideDown))
{
_image1.image = [UIImage imageNamed:#"Portrait.png"];
}
}
I believe it is because the method is only called upon rotation. If I rotate the improper, initial landscape view, for instance, it displays the correct images once again. Is there a way to get the method to run and load the proper landscape view when the initial orientation is in landscape? Or a way to force the correct image to display? Thanks much.

I finally fixed this issue by adding an orientation checker. I added the following in my .h:
#property (nonatomic, readonly) UIDeviceOrientation *orientation;
Then I added this to my .m file in the viewDidLoad method:
if(([[UIDevice currentDevice] orientation] == UIDeviceOrientationLandscapeLeft) || ([[UIDevice currentDevice] orientation] == UIDeviceOrientationLandscapeRight)) {
_image1.image = [UIImage imageNamed:#"Landscape.png"];
}
This checks if the initial orientation is landscape. If it is, it loads my Landscape.png image. Otherwise, since the default image is my Portrait.png, as set in the Storyboard, that loads if the orientation is already in portrait. Cheers!
EDIT: The above code is not advised as you can run into issues when using it, such as with orientation-locked devices. I changed the it to check for the status bar's orientation, rather than the device's orientation, as below:
if(([[UIApplication sharedApplication] statusBarOrientation] == UIInterfaceOrientationLandscapeLeft) ||
([[UIApplication sharedApplication] statusBarOrientation] == UIInterfaceOrientationLandscapeRight)) {
_image1.image = [UIImage imageNamed:#"Landscape.png"];
}
You do not need to declare any variables in the .h, and just add the above in the viewDidLoad method.

Related

iOS7 How to Capture an Image and add a Caption

Using Xcode 5 and Developing for iOS7 How can I capture an image add a caption and then store it within an app to be retrieved and displayed on a separate view controller within the app.
I know you set this code
- (BOOL) startCameraControllerFromViewController: (UIViewController*) controller
usingDelegate: (id <UIImagePickerControllerDelegate,
UINavigationControllerDelegate>) delegate {
if (([UIImagePickerController isSourceTypeAvailable:
UIImagePickerControllerSourceTypeCamera] == NO)
|| (delegate == nil)
|| (controller == nil))
return NO;
UIImagePickerController *cameraUI = [[UIImagePickerController alloc] init];
cameraUI.sourceType = UIImagePickerControllerSourceTypeCamera;
// Displays a control that allows the user to choose picture or
// movie capture, if both are available:
cameraUI.mediaTypes =
[UIImagePickerController availableMediaTypesForSourceType:
UIImagePickerControllerSourceTypeCamera];
// Hides the controls for moving & scaling pictures, or for
// trimming movies. To instead show the controls, use YES.
cameraUI.allowsEditing = NO;
cameraUI.delegate = delegate;
[controller presentModalViewController: cameraUI animated: YES];
return YES;
}
But how do I then caption the image save it a retrieve it on another view controller, would really appreciate the help.
If i am not wrong, There is no in build property or API for adding caption in image.
First you have to capture image using UIImagePickerController after capturing image use your UIViewcontroller that shows image and put there UITextfield to add caption , and then after save it.
Or You can use Overlay property of UIImagePickerController you can try with it.

XCode Change Background Image with Orientation Change

I have an iPhone app made up of a single UIWebView that displays a website. I have set a background image to the UIWebView after it finishes loading within my ViewController.m file, as shown here:
- (void)webViewDidFinishLoad:(UIWebView *)webView {
// Set UIWebView Background Image
self->webView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"ui-background-portrait.png"]];
}
This is fine when the device is in Portrait orientation, but I would like to change the background image if the user switches the device orientation to landscape, and back to portrait if they switch again, etc.
I wrote the code below, but I'm not sure where to put it (as it needs to change the background image whenever they switch orientations; not just one time):
- (void)webViewDidFinishLoad:(UIWebView *)webView {
// Set UIWebView Background Image
if (UIDeviceOrientationIsPortrait([UIDevice currentDevice].orientation))
{
// code for Portrait orientation
self->webView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"ui-background-portrait.png"]];
}
if (UIDeviceOrientationIsLandscape([UIDevice currentDevice].orientation))
{
// code for landscape orientation
self->webView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"ui-background-landscape.png"]];
}
}
How can I accomplish this? And if you could give me code and indicate where to put it, that would be such a great help :)
Override one of these methods in your view controller and put your code there.
– willRotateToInterfaceOrientation:duration:
– willAnimateRotationToInterfaceOrientation:duration:
– didRotateFromInterfaceOrientation:
Put this code in your ViewController.m
- (void) didRotateFromInterfaceOrientation:(UIInterfaceOrientation) fromInterfaceOrientation
{
// Set UIWebView Background Image
if (UIDeviceOrientationIsPortrait([UIDevice currentDevice].orientation))
{
// code for Portrait orientation
self->webView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"ui-background-portrait.png"]];
}
if (UIDeviceOrientationIsLandscape([UIDevice currentDevice].orientation))
{
// code for landscape orientation
self->webView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"ui-background-landscape.png"]];
}
}

UIInterfaceOrientaion not following app rules

I've been having a huge problem with this.
Xcode just simply turned my landscape mode app into a portrait view and It doesn't go back !
I've programmed the entire app almost to run in landscape mode in the Ipad.
On the Storyboard, every window is in lanscape.
I believe i did the settings correctly according to the images below
and finally on my viewController I have:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return YES;
}
Anybody know what could be going wrong or why would Xcode just set the screen to portrait out of nowhere while I was just adjusting a viewController?
If you want to only support landscape then change shouldAutorotateToInterfaceOrientation to:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
if (interfaceOrientation == UIInterfaceOrientationLandscapeLeft ||
interfaceOrientation == UIInterfaceOrientationLandscapeRight) {
return YES;
}
// Default
return NO;
}

iPad: How to display a different screen depending on orientation (landscape / portrait)

I have an iPad application that can be used in all four view modes (portrait up/down and landscape left/right). But at a certain point I have a View that I only want to be seen in landscape mode. So I do the following in the UIViewController that will trigger the action to view the landscape-only view:
- (void) showProperty:(Property *) property {
if ([self interfaceOrientation] == UIInterfaceOrientationLandscapeLeft || [self interfaceOrientation] == UIInterfaceOrientationLandscapeRight) {
PropertyViewController *propertyView = [[PropertyViewController alloc] initWithNibName:#"PropertyViewController" bundle:[NSBundle mainBundle]];
propertyView.property = property;
[self.navigationController pushViewController:propertyView animated:YES];
[propertyView release];
propertyView = nil;
}
else {
RotateDeviceViewController *rotateView = [[RotateDeviceViewController alloc] initWithNibName:#"TabRotate" bundle: [NSBundle mainBundle]];
rotateView.property = property;
[self.navigationController pushViewController:rotateView animated:YES];
[rotateView release];
rotateView = nil;
}
}
This works fine and thus shows either the desired screen (PropertyViewController) when the iPad is held in landscape mode, and if not it shows the RotateDeviceViewController which shows the user a message that he/she is supposed to rotate the device to correctly view the screen.
So when the user then rotates his/her device to landscape mode I want to show them the right view (PropertyViewController). And all of this kinda works!
The problem arises though in this RotateDeviceViewController.. There I have the following:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
if (UIInterfaceOrientationIsLandscape(interfaceOrientation))
[self showProperty];
return UIInterfaceOrientationIsPortrait(interfaceOrientation);
}
- (void) showProperty {
PropertyViewController *propertyView = [[PropertyViewController alloc] initWithNibName:#"PropertyViewController" bundle:[NSBundle mainBundle]];
propertyView.property = property;
[self.navigationController pushViewController:propertyView animated:YES];
[propertyView release];
}
So as soon as I rotate the device (when viewing the RotateDeviceViewController) to landscape mode I show the user the PropertyViewController. This works... But when the PropertyViewController appears it shows my layout 90 degrees rotated. So basically it shows the content in portrait mode instead of using the landscape mode (which is actually the way you are holding the device)..
I hope this makes sense and someone can show me what's causing this.
Screenshots to make it more clear:
When device is held in portrait mode
After rotating the device
At this point
- (BOOL)shouldAutorotateToInterfaceOrientation:UIInterfaceOrientation)interfaceOrientation
You are telling the view controller what orientations you support. The device has not actually rotated yet therefore the view controllers intefaceOrientation property will still be portrait so when it is pushed onto the stack it thinks the device is portrait.
pseudo code
shouldAutoRotate... // at this point self.interfaceOrientation == portrait
// you push your controller here so it loads when the property is
I'm not sure if this will work well but the earliest I can see you can push is in
- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation

Best way to change UIViewController view NIB programmatically

I have this iPad application with different NIBs and views.
Each view has two NIBs, one for potrait orientation and one for landscape, so i'm searching for a method to programmatically switch NIB for a given UIViewController .
Right now i have this function that i'm using in the willRotateToInterfaceOrientation method of each controller :
void UIHandleRotation( UIViewController *controller, NSString *nibName, UIInterfaceOrientation orientation, BOOL transform ){
double angle = 0.0;
if( orientation == UIInterfaceOrientationPortrait ) {
angle = PI * 2 /* 360° */;
}
else if( orientation == UIInterfaceOrientationLandscapeLeft ){
angle = PI + PI/2 /* 270 ° */;
// Each landscape nib is [nib-name]L
nibName = [NSString stringWithFormat:#"%#L", nibName];
}
[[NSBundle mainBundle] loadNibNamed:nibName owner:controller options:nil];
if( transform ) {
controller.view.transform = CGAffineTransformMakeRotation( angle );
}
}
But it's giving me strange behaviour (ui controls position messed, the outlets are not associated as in interface builder, and so forth).
Am i doing something wrong or there's just a better way to implement this ?
Thanks
NOTE: I'm not using a navigation controller, so i can't use this solution Easiest way to support multiple orientations? How do I load a custom NIB when the application is in Landscape?
You should use only one NIB with two views, one for portrait(potraitView) with frame(0,0,768, 1024)and
another for landscape(landScapeView) with frame(0,0,1024, 768).
And set your controls in both views (potraitView and landScapeView) according to your requirements.
and after this you can set your views according to device orientation as-
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
if(interfaceOrientation == UIInterfaceOrientationPortrait || interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown)
{
landScapeView.hidden = YES;
portraitView.hidden = NO;
}
else{
landScapeView.hidden = NO;
portraitView.hidden = YES;
}
return (interfaceOrientation == UIInterfaceOrientationPortrait || interfaceOrientation == UIInterfaceOrientationLandscapeLeft ||
interfaceOrientation == UIInterfaceOrientationLandscapeRight || interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown);
}
Hi,
As I think you want to know, when the view loads, what is the default orientation, and How I can load the view according to this orientation.
If this question is same as you are asking, then the answes is-
When you run your application and the same class view loads, then - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
method is called automatically. Then it returns your device orientation, and sets your view according to this orientation(using the same code as described above).
I think you need not to do more.
You shouldn't switch nibs, you should switch controllers.
See this answer for: Easiest Way to Support Multiple Orientations
you need to have one view per nib, so your UIViewController will have two UIViews. one for portrait, and one for landscape.
Why do you need to use 2 nibs for the same view.You should use one nib for different orientations.You can handle the required controls programatically in the same method
willRotateToInterfaceOrientation
Thanks.

Resources