UIScrollView behaving differently in portrait mode and landscape mode - iphone

i have created a UIScrollVIew(name-helpView) through XIB with the a size of (768,1800).
Now i am adding this to another view through the following code.
-(IBAction)showHelpView:(UIButton *)sender{
if(helpViewIsShowing==NO){
//set the content size more than the view size to make the view scroll
helpView.contentSize = CGSizeMake(helpView.frame.size.width, 2200);
[self.view addSubview:helpView];
helpButton.selected=YES;
helpView.backgroundColor=[UIColor whiteColor];
helpView.frame=CGRectMake(0, 41, helpView.frame.size.width, helpView.frame.size.height);
helpViewIsShowing=YES;
}
else{
[helpView removeFromSuperview];
helpButton.selected=NO;
helpViewIsShowing=NO;
}
}
The frame of the scrollview is appearing fine when i run it in portrait mode. Itz also working fine if i turn the device from to PORTRAIT TO LANDSCAPE. But if i run the the code in landscape mode, then the frame of the scrollview is not adjusting itself to the full screen size. i have also given a auto resizing mask through XIB. But no luck either.
can anyone help me with this please.... Thanku

try to set the view frame in your view controller this function to the view size your required.you can define height and width according to your requirement.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Override to allow orientations other than the default portrait orientation.
if (interfaceOrientation==UIInterfaceOrientationLandscapeLeft || interfaceOrientation==UIInterfaceOrientationLandscapeRight) {
self.helpView.frame = CGRectMake(0, 0, 703,768);
} else {
self.helpView.frame = CGRectMake(0, 0, 768, 1024);
}
return YES;
}
good luck

Related

iOS textviews not expanding in scrollview in landscape

I had a view with some textviews in it that were working as I wanted. The expanded to a the same right-margin whether in landscape or portrait.
I recently have tried changing the normal view to a scrollview. I've had no luck getting these text views to expand as they once did, though. When in landscape mode everything stays huddled over on the left side with the same width as a portrait phone.
Here is some code.
- (void)viewDidLoad {
CGSize screen = [self handleScreenOrientation];
[(UIScrollView *)self.view setContentSize:CGSizeMake(screen.width, screen.height)];
}
- (CGSize)handleScreenOrientation {
UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];
CGRect screenRect = [[UIScreen mainScreen] bounds];
if (orientation == UIDeviceOrientationPortrait || orientation == UIDeviceOrientationPortraitUpsideDown ) {
return CGSizeMake(screenRect.size.width, screenRect.size.height);
}
else {
return CGSizeMake(screenRect.size.height, screenRect.size.width);
}
}
- (void) willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
CGSize screen = [self handleScreenOrientation:toInterfaceOrientation];
UIScrollView *scrollView = (UIScrollView *)self.view;
scrollView.frame = CGRectMake(0, 0, screen.width, screen.height);
scrollView.contentSize = CGSizeMake(scrollView.frame.size.width, scrollView.frame.size.height);
[scrollView setNeedsDisplay];
}
The method handleScreenOrientation with the passed orientation is the same as the one w/ no parameters, just it uses the passed orientation instead of the current orientation of the status bar.
I've checked and my scrollview is set to autoresize subviews.
Any ideas would be much appreciated. Thanks.
Update: I have added
self.view.autoresizesSubviews = true;
for (UIView *view in self.view.subviews) {
view.autoresizingMask = UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleWidth;
}
to viewdidload and willrotatetointerfaceorientation. No change.
I think the problem is that you are overriding the autoresizingMasks by forcing a change to the scrollView's contentSize. Although you have correctly noted that the screenSize is always in portrait orientation, so you reverse its dimensions for landscape, you are determining which orientation to use before the device actually rotates. So you're forcing the scrollView to maintain portrait dimensions in landscape.
Keep your autoresizeMask routine and discard your willRotateToInterfaceOrientation routine. Does it work now?
If not, try putting the willRotate code into *did*RotateToInterfaceOrientation.
Another idea:
I notice that in viewDidLoad you cast the container view (self.view) to a UIScrollView. I wonder if it might be better to leave the container view as a generic UIView, add a UIScrollView as a subview, and then add the textviews to the scrollview.
I might be out in left field here, but I've never used a scrollview directly at the container-view level, and I wonder if that might be the problem.

How to prevent size of UIScrollView after device orientation is changed?

I have UIScrollView on xib-file on portrait mode. When I run the application and rotate device to landscape mode I see the new bounds of the UIScrollView. Half of the controls inside UIScrollView at the bottom are inaccessible. There is no any scroller visible.
How to prevent change of the vertical size of UIScrollView after device orientation is changed?
Thanks a lot for the help!
Change the UIScrollView's frame in
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation;
According to the device rotation.
shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
if(interfaceOrientation == UIInterfaceOrientationPortrait)
{
//set frame for scroll in portrait mode
}
else if(interfaceOrientation == UIInterfaceOrientationLandscapeLeft)
{
//set frame for landscape
}
}
select your UIScrollView and then from properties click on size inspector and change autoresize property for your view.
EDIT : select your UIScrollView and change autoresize property for your scroll view like this.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation;
{
//change sub view of scrollview accordingly to orientation
if(UIInterfaceOrientationIsPortrait(interfaceOrientation))
yourScrollView.frame = CGRectMake(0,0,320,460);
yourScrollView.contentSize = CGSizeMake(320,460);//change accordingly
else
yourScrollView.frame = CGRectMake(0,0,480,300);
yourScrollView.contentSize = CGSizeMake(480,460); //change accordingly
return YES;
}
Setup Reference Outlet.
Use the code:
scrollView.contentSize = CGSizeMake(width, height);

UIImage/UIView Doesn't Adjust Properly To UIInterfaceLandscape Views

I am writing an app that I would like to only be viewed in Landscape Mode. I have set it up so that if the iPhone is held in Portrait Mode, nothing happens and the current image remains in Landscape Mode. The iPhone Simulator starts out in Landscape Mode with the Home Button on the right. If the iPhone is rotated from one Landscape Mode to the other, animation then occurs and the view is adjusted. However, whenever the device is in Landscape Mode with the Home Button on the left, the image is 20 pixels higher than needed, revealing a white line at the bottom of the screen.
In spite of all attempts to logically adjust this such as
self.view.frame = CGRectMake (0,20, self.view.frame.size.width, self.view.frame.size.height)
it doesn't fix the problem. I am accounting for the Status Bar in my calculations.
The .xib file contains an UIImageView on top of a UIView. This is my first experience implementing these methods so I apologize if the solution is relatively easy. Below is the code for the two methods used to implement the Landscape Mode views.
-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
//set up interface to only be viewed in Landscape
if(interfaceOrientation == UIInterfaceOrientationLandscapeLeft)
return interfaceOrientation;
else if(interfaceOrientation == UIInterfaceOrientationLandscapeRight)
return interfaceOrientation;
else
return NO;
}
-(void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)tointerfaceOrientation duration:(NSTimeInterval)duration {
if(UIInterfaceOrientationLandscapeRight){
self.view.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
}
else if (UIInterfaceOrientationLandscapeLeft) {
//shouldn't adjustment to 20 fix the view?
self.view.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
}
else return;
}
For UIViews to adjust the subviews properly you will have to take a look at this UIView property: autoresizingMask, and set the mask to autoresize what is needed, if that does not work you will have to override:
- (void)layoutSubviews
From the reference: UIView reference

uitextfied autosize for Landscape if it before in Portrait view

i don't know about this:
is it possible that if user switch to Landscape view then size of uitextfied change as i have 4 text file in view ....just look and feel
If you are using IB you have two ways of doing it.
using the auto resize properties in object inspector of Interface Builder. That will resize your textfields automatically.
If that does not suffice your needs, you will have to control that in your ViewController method
-(void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation {
if (UIInterfaceOrientationIsPortrait (fromInterfaceOrientation)) {
txtField.frame = CGRectMake(0, 0, 400, 31);
}
else {
//orientation for portrait
}
}
yes you can change the size of the uitextfied by using its "frame" property
txtField.frame = CGRectMake(0, 0, 400, 31);
based on the condition of the device orientation change the size

Resizing UINavigationBar on rotation

I have a subclass of UIViewController which handles a UIView. The viewcontroller is presented modally (it slides up from the bottom of the screen). At the top of the view, i have added a navigation bar. Note that this bar is not handled by a navigation controller.
I want to get the navbar to shrink in height when the view rotates to landscape (similar to how it behaves when it is handled by a UINavigationController). However, I can't set its autoresizing mask to flexible height in IB, and doing so in code causes the navbar to disappear completely.
Is there a way to do this? How is it done by the UINavigationController?
P.S. I would prefer not having to resort to a scaling transform, since this would mess up the text in the title.
EDIT: I solved it with a little help, read the answer posted below.
Rather than set it's autoresizing mask, why don't you just check the current orientation in viewWillAppear, as well as in didRotateFromInterfaceOrientation, and set the appropriate frame?
- (void) updateNavBar {
UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];
if ((UIInterfaceOrientationLandscapeLeft == orientation) ||
(UIInterfaceOrientationLandscapeRight == orientation)) {
myNavBar.frame = CGRectMake(0, 0, 480, 34);
} else {
myNavBar.frame = CGRectMake(0, 0, 320, 44);
}
}
- (void) viewWillAppear {
[self updateNavBar];
// ... SNIP ...
}
- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation {
[self updateNavBar];
// ... SNIP ...
}
I found the solution, and in hindsight i feel rather stupid. I just had to include flexible bottom margin in the navbar's autoresize mask. Credit is due to user RayNewbie in this thread, which pointed me to the solution:
http://discussions.apple.com/thread.jspa?messageID=8295525