Programmatically toggle ability to rotate orientation - objective-c

I am making an app that allows you to free draw ("annotate") over a background image of a music sheet. My app allows me to toggle the drawing to hide/unhide. When I rotate the orientation to landscape, the drawing and the background image do not align in the way that they did for portrait mode, so I would like to prevent orientation change when the drawing is not hidden and re-allow it if the drawing is hidden.
Is this possible? Any help would be much appreciated.

In your view controller do this:
-(UIInterfaceOrientationMask)supportedInterfaceOrientations
{
if ( drawingView.hidden )
return UIInterfaceOrientationMaskAll;
else
return [[UIApplication sharedApplication] statusBarOrientation];
}

Related

how to disable portrait mode for the components when device rotates in Objective C

I have a background Image, to a view of size full screen.
This code creates the view:
UIImageView *backgroundView=[[UIImageView alloc]initWithFrame:self.view.bounds];
backgroundView.image=[UIImage imageNamed:#"fullscreen.jpg"];
When I rotate my phone to landscape its dimensions change. So how can I disable landscape rotation of my view so that for any rotation its frame will not change.
The view in question:
Lets to see general property of project -> Deployment Info (checkbox "Landscape Left" and "Landscape Right"). I hope this will help.
Programmatically:
You can override method "supportedInterfaceOrientations"
something like this:
- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
return UIInterfaceOrientationMaskPortrait;
}

UIImagePickerController live preview orientation wrong

I have a landscape iPad app and I present a UIImagePickerController with sourcetype UIImagePickerControllerSourceTypeCamera to my active view controller modally. However, the live preview is in the wrong orientation and when I take a picture with the wrong orientation, it outputs an image with the correct orientation.
When I rotate the iPad, it fixes the orientation but it makes a thick black bar on one of the sides of the screen depending on the orientation (the camera overlay view isn't cut off by this black bar... only the preview is).
Everything was fine with iOS5, but iOS6 messed up the camera.
How do I fix this live preview orientation bug?
In order to fix the black screen at the bottom of the screen, you have to do a translation and scale, based on the device(iPad or iPhone).
CGAffineTransform translate = CGAffineTransformMakeTranslation(0.0, 25.0);
self.picker.cameraViewTransform = CGAffineTransformScale(translate, 480.0/380.0, 480.0/380.0);
self.picker.cameraOverlayView = self.view;
Try to change the scale values based on the device to make the black bar dissappear.
try this:
dispatch_async(dispatch_get_main_queue(), ^{
[self presentModalViewController:yourUIImagePickerControlle animated:YES];
});

Vertical UIToolBar and also its UIBarButtonItem

In my application i have one UIToolBar. When in portrait mode this toolbar is on top and is horizontal, but when the device is rotated this toolbar should be converted to vertical toolbar and should be place on left side. Also its subviews i.e 5 UIBarButtonItems should also be placed accordingly.
Does any one know the solution for this ?
I have read this for reference, But my tool bar should align itself according to the orientation. I am using iOS 6.
There is no built-in vertical menu feature (yet). There are 2 parts in your question:
Vertical UIToolbar:
You may restrict the device orientation and the toolbar will remain at the same location
- (NSUInteger)supportedInterfaceOrientations {
return UIInterfaceOrientationMaskPortrait;
}
(this will affect all views)
Or you can listen for orientation changes and rotate your toolbar accordingly using its transform property, e.g.
toolbar.transform = CGAffineTransformRotate(CGAffineTransformIdentity, M_PI/2);
Rotated UIBarButtonItem:
If you have rotated the toolbar (case 2 above), items will rotate too.
If not, you need to rotate your items. Several post shows how to deal with the fact that as UIBarButtonItem does not extend UIView, it has no transform property (see here). In the end you will have again to listen for orientation changes and rotate the subviews of your toolbar, e.g.
for (UIView *view in toolbar.subviews) {
view.transform = CGAffineTransformRotate(CGAffineTransformIdentity, M_PI/2);
}
Of course apply the rotation related to your orientation (M_PI/2 or -M_PI/2).
I ran into this same issue, so I subclassed UIToolbar and made it into exactly what I wanted it to be.
Here's the GitHub link: https://github.com/fennelouski/NKFToolbar

Rotate one UIViewController in UITabBar application

I have uitabbar application and I Want to rotate just one ViewController with chart in landscape mode. It's possible to do that?
There is no easy way to have only one view in landscape mode, while the others are in landscape, nor an easy way to programmatically switch to landscape mode.
One possible approach would be using a CGAffineTransform to transform your view in your viewWillAppear (i.e., right before the view is shown):
- (void)viewWillAppear:(BOOL)animated; {
//-- Adjust the status bar
[UIApplication sharedApplication].statusBarOrientation = UIInterfaceOrientationLandscapeRight;
//-- Rotate the view
CGAffineTransform toLandscape = CGAffineTransformMakeRotation(degreesToRadian(90));
toLandscape = CGAffineTransformTranslate(toLandscape, +90.0, +90.0 );
[self.view setTransform:toLandscape];
}
Hope this works for you.
Only if it's modal, if it's part of the tabBarController, you need to support orientation change for all your controller

Stop UIToolbar from Disappearing in Landscape Orientation - iPad

Not sure why this happens or how to stop it, my UIToolBar on the details viewcontroller is only visible during portrait view. I want it visible at all orientations. How do I do that? Thank you.
I encountered the same problem by just dragging a UIToolBar on to my view and docking it on the top of the window. It showed up in landscape but not portrait. Interface Builder - at least the one embedded in Xcode 4 - doesn't seem to do the right thing with the resize masks.
While Kshitiz's answer above will work, it has a couple of flaws. As coded, it does not support all four orientations. More importantly, it's not resolution independent.
A better solution is briefly described in enamrik's comment, so credit should go to him/her. Here are the steps:
Select the tool bar in Interface Builder.
Open the Size inspector.
In the Autosizing box, select the left, right and top "i-beams" on the exterior of the square. This keeps the position of the toolbar fixed relative to the sides of the view when the view is resized.
Inside the Autosizing square, select the horizontal line with arrows on both ends. This causes the size of the toolbar to change in sync with the parent view.
in your this function of view controller reset view frame bro
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Override to allow orientations other than the default portrait orientation.
if (interfaceOrientation==UIInterfaceOrientationLandscapeLeft || interfaceOrientation==UIInterfaceOrientationLandscapeRight) {
self.view.frame = CGRectMake(0, 0, 703,768);
} else {
self.view.frame = CGRectMake(0, 0, 768, 1024);
}
return YES;
}
and your tool bar frame too
good luck
Faced the same problem when I add UIPickerView programmatically and add UIToolBar for the PickerView. Just need to add [.flexibleWidth,.flexibleHeight] for the UIPickerView. eg:-
let statTypePicker = UIPickerView()
And then add
self.statTypePicker.autoresizingMask = [.flexibleWidth,.flexibleHeight]