shouldAutorotateToInterfaceOrientation UIToolbar in landscape mode is one pixel off - iphone

I do add a Toolbar to my view like this:
self.myToolbar = [UIToolbar new];
self.myToolbar.barStyle = UIBarStyleDefault;
self.myToolbar.tintColor = [UIColor BAR_COLOR];
self.myToolbar.frame = CGRectMake(0, self.view.bounds.size.height-44, 320, 44);
self.myToolbar.autoresizingMask = (UIViewAutoresizingFlexibleLeftMargin|UIViewAutoresizingFlexibleTopMargin|UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight);
[self.view addSubview:myToolbar];
In the interface builder I set fixed to top and left margin for the view.
Also in the simulated metrics I set Status Bar to Black, Top Bar to Navigation Bar and Bottom Bar to None.
This works perfect in portrait mode. And looks like this:
Now when I rotate to landscapemode the toolbar gets resized and everything BUT it is off the buttom
by one pixel like this:
What could be the cause off this?

The following code is working :
self.myToolbar = [UIToolbar new];
self.myToolbar.barStyle = UIBarStyleDefault;
self.myToolbar.tintColor = [UIColor BAR_COLOR];
CGRect screenRect = [[UIScreen mainScreen] applicationFrame]; //get the rect of the screen
self.myToolbar.frame = CGRectMake(screenRect.origin.x, screenRect.size.height-44, screenRect.size.width, 44);
self.myToolbar.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
[self.view addSubview:myToolbar];
EDIT
It is actually a bug occuring on the iOS5 simulator.

Related

How to set CustomFrame on iCarousel in iphone?

I want to set the frame of iCarousel according to my need. First it shows in the middle of the simulator but i tried my code now it is showing at the top of the simulator.
I am trying this code:
// Try to add custom view for setting height and width of iCarousel
UIView *Carasouel_Customview = [[UIView alloc]initWithFrame:CGRectMake(10, -245, 768, 245)];
[self.view addSubview:Carasouel_Customview];
// Initialize and configure the carousel
//carousel = [[iCarousel alloc] initWithFrame:self.view.bounds];
carousel = [[iCarousel alloc] initWithFrame:Carasouel_Customview.bounds];
//carousel = [[iCarousel alloc] initWithFrame:CGRectMake(20, 200, 179, 245)];
carousel.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
carousel.type = iCarouselTypeRotary;
carousel.dataSource = self;
carousel.delegate = self;
[self.view addSubview:carousel];
Any hints from experts would be very welcome.
self.carousel = [[iCarousel alloc]initWithFrame:CGRectMake(0.0, 0.0, 200.0, 150.0)];
self.carousel.delegate = self;
self.carousel.dataSource = self;
self.carousel.type = iCarouselTypeLinear;
self.carousel.clipsToBounds = YES; //This is main point
[self.view addSubview:self.carousel];
When we set customFrame to iCarousel we get its width of fullScreen. So just set its clipsToBounds property YES :)
You're setting y-coordinate -245 to Carasouel_Customview and then setting carousel.frame with Carasouel_Customview.bounds - here y-coordinate is 0.
You are adding carousel to self.view, not Carasouel_Customview - so what do you expect?
Solution:
Add carousel as subview to Carasouel_Customview or set carousel.frame with custom values.

Hide of tabbar shows whitespace (even if frame is fullscreen)

Trying to make fullscreen view in a tabbarcontroller. It's in landscape. Are able to hide the tabbar with self.tabBarController.tabBar.hidden = YES this leaves a whitespace where the tabbar have been.
Making the tabbarcontroller's view fullscreen with:
self.tabBarController.view.frame = self.view.frame = CGRectMake(0, 0, 480, 320);
[self.tabBarController.view setCenter:CGPointMake(160.0f, 240.0f)];
self.tabBarController.tabBar.hidden = YES;
self.tabBarController.view.backgroundColor = [UIColor redColor];
Then I try to make the viewcontrollers view fullscreen also. But it will not stick, unable to resize view.frame.
self.view.backgroundColor = [UIColor yellowColor];
self.view.frame = CGRectMake(0, 0, 480, 320);
[self.view setCenter:CGPointMake(160.0f, 240.0f)];
Is there anyway to get around this in a nice fashion?!
Image of whitespace (in red) were tabbar is gone
Set hidesBottomBarWhenPushed property to YES before pushing your viewController.
Did you try to play with autoresizingMask property of your view controllers instead of trying to resize manually the view frames ?
self.tabBarController.view.autoresizingMask = (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight);
self.view.autoresizingMask = (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight);

How to display UIWebView with correct zoom scale in landscape

I create UIWebView and set autorotate like this:
- (void)loadView
{
self.view = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 460)] autorelease];
UIWebView *webview = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 320, 460)];
webview.delegate = self;
webview.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"bg_darkgreen_320x312.png"]];
webview.dataDetectorTypes = UIDataDetectorTypeLink;
webview.multipleTouchEnabled = YES;
webview.scalesPageToFit = YES;
webview.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
[webview loadHTMLString:#"<meta name=\"viewport\" content=\"width=320\">content ..." baseURL:nil];
[self.view addSubview:webview];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}
It works perfectly when I rotate it in landscape without zooming. It will display full screen on both rotation.
The problem is if I zoom-out or a little bit zoom-in in portrait mode and then rotate it in landscape, webview still zoom with the same size of portrait mode and it has black space like in picture > http://cl.ly/1o3c4712053C3T2C2H2z
It's width in landscape is correct, 480px (you can see its scrollbar is on most right of screen). I don't understand why webview don't display full screen.
What I do wrong?
Is there any way to fix this?
Please help.
Thanks in advance.
In order to achieve your effect you need to set the zoomScale to 0 on rotation. See my full answer here: UIWebView content not adjusted to new frame after rotation

How to make UIScrollView respect the layout of the containing UIView?

I'm using a UIView to control the layout of my view (along with a view controller). I want UIScrollView to only use half of the vertical screen. That works fine if I use the upper half of the screen, but not the bottom half.
Here's the relevant code from the UIViewController:
- (void)loadView {
CGRect fullFrame = [[UIScreen mainScreen] applicationFrame];
//trying to put the scroll view on the bottom half of the screen, but does not work.
CGRect halfFrame = CGRectMake(0, fullFrame.size.height / 2 ,
fullFrame.size.width, fullFrame.size.height / 2);
//use this instead for the scroll view to go to the top half of the screen (and work properly)
//CGRect halfFrame = CGRectMake(0, 0 , fullFrame.size.width, fullFrame.size.height / 2);
UIScrollView* sv = [[UIScrollView alloc] initWithFrame:halfFrame];
[sv setContentSize:CGSizeMake(3 * halfFrame.size.width, halfFrame.size.height)];
CGRect stencilFrame = halfFrame;
UIView *leftView = [[UIView alloc] initWithFrame:stencilFrame];
stencilFrame.origin.x += stencilFrame.size.width;
UIView *centerView = [[UIView alloc] initWithFrame:stencilFrame];
stencilFrame.origin.x += stencilFrame.size.width;
UIView *rightView = [[UIView alloc] initWithFrame:stencilFrame];
//mix up the colors
[leftView setBackgroundColor:[UIColor redColor]];
[centerView setBackgroundColor:[UIColor greenColor]];
[rightView setBackgroundColor:[UIColor blueColor]];
//add them to the scroll view
[sv addSubview:leftView];
[sv addSubview:centerView];
[sv addSubview:rightView];
//turn on paging
[sv setPagingEnabled:YES];
UIView *containerView = [[UIView alloc]initWithFrame:fullFrame];
[containerView addSubview:sv];
[self setView:containerView];
}
Thank you in advance for any advice or help.
I figured it out. The crux of the problem is that views within the scroll view are initialized with the same frame as the scroll view itself. When the scrollView is initialized with halfFrame, the origin is (0, half the full screen size), which is ok since that is relative to the application window itself. However, the views that are put inside the scrollView (like leftView) are initialized to halfFrame, but in this case the origin is relative to the scrollView, effectively placing them off the screen. Setting the origin to (0,0) fixes this:
CGRect stencilFrame = CGRectMake(0, 0, fullFrame.size.width , fullFrame.size.height / 2);
contentSize must contain the rectangle of the view inside the scroll view. That is, the total size of all scrollable controls within. The frame of the UIScrollView decides how much scrolling is needed to let the user browse everything.
You don't have the "full frame" available if you have a nav bar or a tab bar. In general, code that uses [UIScreen mainScreen] for layout information is probably wrong.
Additionally, the status bar can change size if (for example) a call is in progress or tethering is enabled.
Instead, use any sane value for full frame and enable autoresizing:
CGRect fullFrame = {{0,0}, {320,480}};
...
sv.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleTopMargin;
EDIT: You also probably need to subclass UIScrollView and implement -setFrame: so that it also sets the content size and -layoutSubviews to do the correct layout.

Landscape Screen shifted to right in 20px

I am trying to rotate the screen on the iphone. For example,
First state of screen : is "Portrait".
Then it calls "Landscape" screen. Following code is used for making Landscape screen :
- (void)viewDidLoad {
[super viewDidLoad];
[UIApplication sharedApplication].statusBarOrientation = UIInterfaceOrientationLandscapeRight;
CGFloat angle = 90 * M_PI / 180;
self.view.transform = CGAffineTransformMakeRotation(angle);
self.view.center = [nRangeAppDelegate sharedAppDelegate].window.center;
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 20, 480, 44)];
label.textAlignment = UITextAlignmentLeft;
label.numberOfLines = 2;
label.text = #"12345\n67890";
[self.view addSubview:label];
}
But above code shifted to right in 20 px.
How to make 480x320 view at point 0,0 no shifting ?
Thanks in advance.
Update:
My application navigation based application. All UIViewControllers have one UIView. When call the the view I am trying rotate, this code is used : MyRoratedController *myCtrlr = [[MyRoratedController alloc] initWithNibName:#"MyRoratedController" bundle:nil]; [navigationController pushViewController: myCtrlr animated:NO]; I've changed CGPointMake(160.0, 240.0) by many different values. But no changes.
Your window is shifted because the appDelegate window is not centred on the screen as it is drawn below the status bar
Try this:
self.view.center = CGPointMake(160.0, 240.0);
You must make sure you are adding this view to a normal viewcontroller (ie not a nav controller or similar). The easiest way to do this is to add it to the main application window, like so:
MyRoratedController *myCtrlr = [[MyRoratedController alloc]
initWithNibName:#"MyRoratedController" bundle:nil];
[window addSubview:[myCtrlr view]];
Note that if your app has a status bar shown you will have to change the 160 above to 150