I want to set a larger background for my navigation bar when the screen is switched to landscape mode. So this is what I did in my view controller:
- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
UIDeviceOrientation deviceOrientation = toInterfaceOrientation;
if([[UINavigationBar class] respondsToSelector:#selector(appearance)]) {
[self.navigationController.navigationBar setBackgroundImage: [UIImageHelper createTopBar: deviceOrientation] forBarMetrics: UIBarMetricsDefault];
}
else
[self.navigationController.navigationBar setBackgroundColor: [UIColor colorWithPatternImage: [UIImageHelper createTopBar: [[UIDevice currentDevice] orientation]]]];
}
And this is the createTopBar method:
+ (UIImage *)createTopBar: (UIDeviceOrientation) orientation {
// Create a new image context
CGSize size;
if (orientation == UIInterfaceOrientationLandscapeLeft || orientation == UIInterfaceOrientationLandscapeRight){
if ([UIDevice isiPhone5]) {
size = CGSizeMake(568, 34);
UIGraphicsBeginImageContextWithOptions(CGSizeMake(568, 34), NO, 0.0);
}
else if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
size = CGSizeMake(1024, 44);
UIGraphicsBeginImageContextWithOptions(size, NO, 0.0);
}
else {
size = CGSizeMake(480, 34);
UIGraphicsBeginImageContextWithOptions(CGSizeMake(480, 34), NO, 0.0);
}
}
else{
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
size = CGSizeMake(768, 44);
UIGraphicsBeginImageContextWithOptions(CGSizeMake(768, 44), NO, 0.0);
}
else if ([UIDevice isiPhone5]) {
size = CGSizeMake(320, 44);
UIGraphicsBeginImageContextWithOptions(CGSizeMake(320, 44), NO, 0.0);
}
else {
size = CGSizeMake(320, 44);
UIGraphicsBeginImageContextWithOptions(CGSizeMake(320, 44), NO, 0.0);
}
}
UIImage * image = [UIImage imageNamed: #"top_bar_without_title"];
[image drawInRect:CGRectMake(0, 0, size.width, size.height+4)];
UIImage * destImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return destImage;
}
The result turns out fine in portrait mode:
This also works great in landscape mode in iOS 6:
But this is how it turns out in iOS 7, landscape mode:
You can see the status bar overlaps the navigation bar, and there's some extra space in the bottom of the navigation bar. (On a side note, I have edited the info.plist file to fix the overlapping status bar issue. This only happens when I try to set new background image for the nav bar.) Do you have any suggestion for this problem? If you do, please let me know and thank you.
So thanks to #eagle.dan.1349's answer, I've come up with an idea: I extend the height of navigation bar background to include the height of the status bar, and then start drawing the background image from lower to leave the space for the status bar:
+ (UIImage *)createTopBar: (UIDeviceOrientation) orientation {
// Create a new image context
CGSize size;
if (orientation == UIInterfaceOrientationLandscapeLeft || orientation == UIInterfaceOrientationLandscapeRight){
if ([UIDevice isiPhone5]) {
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7){
size = CGSizeMake(568, 34);
UIGraphicsBeginImageContextWithOptions(CGSizeMake(568, 54), NO, 0.0);
}
else {
size = CGSizeMake(568, 34);
UIGraphicsBeginImageContextWithOptions(CGSizeMake(568, 34), NO, 0.0);
}
}
else if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
size = CGSizeMake(1024, 44);
UIGraphicsBeginImageContextWithOptions(size, NO, 0.0);
}
else {
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7){
size = CGSizeMake(480, 34);
UIGraphicsBeginImageContextWithOptions(CGSizeMake(480, 54), NO, 0.0);
}
else {
size = CGSizeMake(480, 34);
UIGraphicsBeginImageContextWithOptions(CGSizeMake(480, 34), NO, 0.0);
}
}
}
else{
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
size = CGSizeMake(768, 44);
UIGraphicsBeginImageContextWithOptions(CGSizeMake(768, 44), NO, 0.0);
}
else if ([UIDevice isiPhone5]) {
size = CGSizeMake(320, 44);
UIGraphicsBeginImageContextWithOptions(CGSizeMake(320, 44), NO, 0.0);
}
else {
size = CGSizeMake(320, 44);
UIGraphicsBeginImageContextWithOptions(CGSizeMake(320, 44), NO, 0.0);
}
}
UIImage * image = [UIImage imageNamed: #"top_bar_without_title"];
if ((orientation == UIInterfaceOrientationLandscapeLeft ||
orientation == UIInterfaceOrientationLandscapeRight) &&
[[[UIDevice currentDevice] systemVersion] floatValue] >= 7){
[[UIColor blackColor] set];
UIRectFill(CGRectMake(0, 0, size.width, 40));
[image drawInRect:CGRectMake(0, 20, size.width, size.height+4)];
}
else {
[image drawInRect:CGRectMake(0, 0, size.width, size.height+4)];
}
UIImage * destImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return destImage;
}
And voila, it works like a charm! Really hope this could be somehow helpful for someone who's trying to hold on the iOS 6's status bar style like me.
iOS 7 have quite a lot of undocumented behavior in terms of system controls and appearance. I suggest you 'give up' fighting the status bar and adjust your image to fit well under it. You can try just adding 20px of black color at the top of the image.
Related
I rotated a view using CGAffineTransformMakeRotation. ( iPhone - allow landscape orientation on just one viewcontroller )
As you can see below, the images have white region in left and right.
I want the image take up the whole space with black background.(at least in one dimension, width or height )
Below is the full code
- (void) viewDidLoad
{
[super viewDidLoad];
self.view.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
self.view.backgroundColor = [UIColor blackColor];
self.imageView.backgroundColor = [UIColor blackColor];
self.imageView.opaque = NO;
self.imageView.contentMode = UIViewContentModeScaleAspectFit;
self.imageView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth |
UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin;
[self.imageView setImageWithURL:[NSURL URLWithString:self.jsonAlbumImage.url_image
relativeToURL: [NSURL URLWithString:#URL_BASE]]
placeholderImage: [GlobalHelper placeHolderImage]];
[self.view addSubview: self.imageView];
}
- (void)didRotate:(NSNotification *)notification {
UIDeviceOrientation orientation = [[notification object] orientation];
if (orientation == UIDeviceOrientationLandscapeLeft) {
[self.view setTransform:CGAffineTransformMakeRotation(M_PI / 2.0)];
} else if (orientation == UIDeviceOrientationLandscapeRight) {
[self.view setTransform:CGAffineTransformMakeRotation(M_PI / -2.0)];
} else if (orientation == UIDeviceOrientationPortraitUpsideDown) {
[self.view setTransform:CGAffineTransformMakeRotation(M_PI)];
} else if (orientation == UIDeviceOrientationPortrait) {
[self.view setTransform:CGAffineTransformMakeRotation(0.0)];
}
}
-- EDIT --
What worked for me in the end .. don't know why modification does work.. any explanation would be great!
UIDeviceOrientation orientation = [[notification object] orientation];
CGAffineTransform t;
CGRect rect;
if (orientation == UIDeviceOrientationLandscapeLeft) {
t = CGAffineTransformMakeRotation(M_PI / 2.0);
rect = CGRectMake(0,0,480,320);
} else if (orientation == UIDeviceOrientationLandscapeRight) {
t = CGAffineTransformMakeRotation(M_PI / -2.0);
rect = CGRectMake(0,0,480,320);
} else if (orientation == UIDeviceOrientationPortraitUpsideDown) {
t = CGAffineTransformMakeRotation(M_PI);
rect = CGRectMake(0,0,320,480);
} else if (orientation == UIDeviceOrientationPortrait) {
t = CGAffineTransformMakeRotation(0.0);
rect = CGRectMake(0,0,320,480);
}
else
return; // looks like there are other orientations than the specified 4
[self.view setTransform:t];
self.view.bounds = rect;
In - (void)didRotate:(NSNotification *)notification, you need to relayout your views so that they occupy all the space available. You could do something like this:
- (void)didRotate:(NSNotification *)notification {
UIDeviceOrientation orientation = [[notification object] orientation];
CGAffineTransform t;
if (orientation == UIDeviceOrientationLandscapeLeft) {
t = CGAffineTransformMakeRotation(M_PI / 2.0);
} else if (orientation == UIDeviceOrientationLandscapeRight) {
t = CGAffineTransformMakeRotation(M_PI / -2.0);
} else if (orientation == UIDeviceOrientationPortraitUpsideDown) {
t = CGAffineTransformMakeRotation(M_PI);
} else if (orientation == UIDeviceOrientationPortrait) {
t = CGAffineTransformMakeRotation(0.0);
}
CGPoint screenCenter = CGPointMake([UIScreen mainScreen].bounds.width/2,[UIScreen mainScreen].bounds.height/2);
self.view.center = CGPointApplyAffineTransform(screenCenter, t);
self.view.bounds = CGRectApplyAffineTransform([UIScreen mainScreen].bounds, t);
[self.view setTransform:t];
}
Where :
CGRectApplyAffineTransform
Applies an affine transform to a rectangle.
CGRect CGRectApplyAffineTransform (
CGRect rect,
CGAffineTransform t
);
Try also removing this line:
self.view.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
You don´t need it if you are not going to use autorotation and I fear it might conflict with your own setting the view's frame. This is just an hypothesis to account for the fact that you are not seeing the view´s frame change.
EDIT:
I'd very much like to know what this transform thing does
well, the transform is doing the rotation.
rotating is relative to an anchor point which is used as a pivot; what happens is that if the anchor point is not in the middle of the view being rotated, then the view is also translated (imagine a rotation around a vertex).
So, it is correct to set the bounds to make things even; indeed I was suggesting just that with the lines:
self.view.center = CGPointApplyAffineTransform(screenCenter, t);
self.view.bounds = CGRectApplyAffineTransform([UIScreen mainScreen].bounds, t);
but possibly the idea of applying the same transform to both the center and the bounds was not blessed. (that is also why I asked for some traces, to see what was happening :-)
I have 2 images, one in portrait mode, and the other in landscape mode.
What is the best way to switch these images when a mobile device view rotation takes place?
Currently I just display the portrait image. And when the device rotates to landscape mode the portrait image is simply stretched.
Should I be checking within the orientation rotation handler and simply reset the image to the proper orientational image (i.e. set it manually based on the orientation)??
Thanks!
I found three ways.I think the last one is better
1: Autoresizing
Example:
UIImageView *myImageView=[[UIImageView alloc] initWithImage:[UIImage imageNamed:#"yourImage.png"]];
myImageView.frame = self.view.bounds;
myImageView.autoresizingMask=UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight
myImageView.contentMode = UIViewContentModeScaleAspectFill;
[self.view addSubview:myImageView];
[imageView release];
2: CGAffineTransformMakeRotation
Example:
-(void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
duration:(NSTimeInterval)duration {
if (toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft) {
myImageView.transform = CGAffineTransformMakeRotation(M_PI / 2);
}
else if (toInterfaceOrientation == UIInterfaceOrientationLandscapeRight){
myImageView.transform = CGAffineTransformMakeRotation(-M_PI / 2);
}
else {
myImageView.transform = CGAffineTransformMakeRotation(0.0);
}
}
3:Set autosizing of myImageView as auto fill the screen in Interface Builder
Example:
-(void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation {
if((self.interfaceOrientation == UIDeviceOrientationLandscapeLeft) || (self.interfaceOrientation == UIDeviceOrientationLandscapeRight)){
myImageView.image = [UIImage imageNamed:#"myImage-landscape.png"];
} else if((self.interfaceOrientation == UIDeviceOrientationPortrait) || (self.interfaceOrientation == UIDeviceOrientationPortraitUpsideDown)){
myImageView.image = [UIImage imageNamed:#"myImage-portrait.png"];
} }
see more solutions here
developer.apple solution is here
Ok so I currently have 3 views and I need only one of them to autorotate to any orientation while the rest stay in portrait. Right now my set up is a splashviewcontroller fades into view A, and inside view A is a button to switch to view B. All I want is for view B to be able to rotate to any orientation.
When I return YES for shouldautorotatetointerfaceorientation in the splashviewcontroller, every view rotates because this is the parent view. When I return portrait only in the splashview, nothing rotates even if the other views return YES. Is there a way to only have view B rotate? I'm willing to do it manually if you can provide code. Thanks
You can manually mange the rotation of any desired UIView object like so:
EDIT:
In the init or viewDidLoad
- (void)viewDidLoad
{
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(rotate) name:UIDeviceOrientationDidChangeNotification object:nil];
[super viewDidLoad];
}
#define degreesToRadian(x) (M_PI * (x) / 180.0)
-(void)rotate{
self.view.bounds = CGRectMake(0, 0, 0, 0);
if ([UIDevice currentDevice].orientation == UIInterfaceOrientationPortrait){
CGAffineTransform landscapeTransform = CGAffineTransformMakeRotation(degreesToRadian(0));
landscapeTransform = CGAffineTransformTranslate (landscapeTransform, 0.0, 0.0);
self.view.bounds = CGRectMake(self.view.bounds.origin.x, self.view.bounds.origin.y, 320, 480);
[self.view setTransform:landscapeTransform];
} else if ([UIDevice currentDevice].orientation == UIInterfaceOrientationPortraitUpsideDown){
CGAffineTransform landscapeTransform = CGAffineTransformMakeRotation(degreesToRadian(180));
landscapeTransform = CGAffineTransformTranslate (landscapeTransform, 0.0, 0.0);
self.view.bounds = CGRectMake(self.view.bounds.origin.x, self.view.bounds.origin.y, 320, 480);
[self.view setTransform:landscapeTransform];
} else if ([UIDevice currentDevice].orientation == UIInterfaceOrientationLandscapeRight){
CGAffineTransform landscapeTransform = CGAffineTransformMakeRotation(degreesToRadian(90));
landscapeTransform = CGAffineTransformTranslate (landscapeTransform, 0.0, 0.0);
self.view.bounds = CGRectMake(self.view.bounds.origin.x, self.view.bounds.origin.y, 480, 320);
[self.view setTransform:landscapeTransform];
}else if ([UIDevice currentDevice].orientation == UIInterfaceOrientationLandscapeLeft){
CGAffineTransform landscapeTransform = CGAffineTransformMakeRotation(degreesToRadian(270));
landscapeTransform = CGAffineTransformTranslate (landscapeTransform, 0.0, 0.0);
self.view.bounds = CGRectMake(self.view.bounds.origin.x, self.view.bounds.origin.y, 480, 320);
[self.view setTransform:landscapeTransform];
}
}
Inside targets summary section, Supported Interface Orientation all items are selected except for Upside Down, so all you need to do is go to your .m file that handles the view and use this piece of code.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
This did the trick for me.
Is the code below correct? When the user rotates the device, two labels are supposed to go to the coordinates given below. It works when the user starts the app in portrait mode, the labels are placed correctly. However, when the user starts in landscape mode, the labels DO NOT get placed correctly. But if you rotate the view to portrait and then back to landscape, they align properly. I've tried placing the landscape coordinates in viewDidLoad, and it still doesn't work. What should I do? Thanks for your help!
The two labels are recordingTimeLabel and recordingTimeLabelMinutes.
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
if (toInterfaceOrientation == UIInterfaceOrientationLandscapeRight || toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft) {
//is landscape
backGround.frame = CGRectMake(0, 0, 768, 1024);
recordingTimeLabel.center = CGPointMake(967, 22);
recordingTimeLabelMinutes.center = CGPointMake(901, 22);
NSLog(#"is landscape");
// fixedSpace.width = 400;
} else {
//is portrait
backGround.frame = CGRectMake(0, 0, 1024, 768);
recordingTimeLabel.center = CGPointMake(710, 22);
recordingTimeLabelMinutes.center = CGPointMake(661, 22);
NSLog(#"is portrait");
}
}
Additionally, this code doesn't work either:
-(void)viewWillAppear:(BOOL)animated {
if (([[UIDevice currentDevice] orientation] == UIInterfaceOrientationLandscapeRight) || ([[UIDevice currentDevice] orientation] == UIInterfaceOrientationLandscapeLeft)) {
//is landscape
backGround.frame = CGRectMake(0, 0, 768, 1024);
recordingTimeLabel.center = CGPointMake(967, 22);
recordingTimeLabelMinutes.center = CGPointMake(901, 22);
NSLog(#"is landscape");
} else {
//is portrait
backGround.frame = CGRectMake(0, 0, 1024, 768);
recordingTimeLabel.center = CGPointMake(710, 22);
recordingTimeLabelMinutes.center = CGPointMake(661, 22);
NSLog(#"is portrait");
}
}
willRotateToInterfaceOrientation: may not be called if you start in landscape mode. I suggest setting the coordinates in viewWillAppear:, not viewDidLoad, to figure out the initial orientation (you can use self.interfaceOrientation if you have autorotation enabled).
Have you set following code also for the UIInterfaceOrientation?
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return YES;
}
Got it. I used an NSTimer and called a function very often that contained this code:
-(void)updateLabelLocation {
if (([[UIDevice currentDevice] orientation] == UIInterfaceOrientationLandscapeRight) || ([[UIDevice currentDevice] orientation] == UIInterfaceOrientationLandscapeLeft)) {
recordingTimeLabel.center = CGPointMake(710, 22);
recordingTimeLabelMinutes.center = CGPointMake(661, 22);
}
}
I made a UIViewController, which programatically generates a UIScrollView. Everything's fine, but when I rotate the Device, the UIScollView should resize so it takes the complete width of my View.
Is there a way to do that without rebuilding the complete UIScrollView ?
Thx a lot !
Sebastian
This is called in my viewDidLoad:
-(void)buildmyScroller {
myScroller = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 800, 768, 100)];
//...adding some subviews to myScroller
thumbScroller.contentSize = CGSizeMake(3000, 100);
[[self view] addSubview:myScroller];
}
Then I tried to resize myScroller with this, when I used setFrame, I said myScroller would not respond to it... :
-(void)changemyScroller {
UIInterfaceOrientation interfaceOrientation = self.interfaceOrientation;
if (interfaceOrientation == UIInterfaceOrientationPortrait) {
[thumbScroller setFrame:CGRectMake(0, 805, 768, 150)];
}
else if (interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown){
thumbScroller.frame = CGRectMake(0, 805, 768, 150);
}
else if (interfaceOrientation == UIInterfaceOrientationLandscapeLeft){
thumbScroller.frame = CGRectMake(0, 549, 1024, 150);
}
else if (interfaceOrientation == UIInterfaceOrientationLandscapeRight){
thumbScroller.frame = CGRectMake(0, 549, 1024, 150);
}
}
And called the method in didAnimateFirstHalf... cause I'm not shure where else to call it.
Thx a lot again !!
[scrollView setFrame:CGRectmake(x, y, width, height)];
//Maybe you need to do the same for the content of the scrollView to make it fit your layout
should do it. You can wrap that in an UIAnimation block if it need to be a transition.
Try this:
if(self.rowNumber == 0){
/******************* Scroller Setup *****************/
// how many pages
int pageCount = 5;
//set up the scrollView
UIScrollView *scroller = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 768, 960)];
// support for Landscape Orienation
if(UIInterfaceOrientationLandscapeLeft){
[scroller setFrame:CGRectMake(0,0,1024, 704)];
}
if(UIInterfaceOrientationLandscapeRight){
[scroller setFrame:CGRectMake(0,0,1024, 704)];
}