Autoresize UIScrollView - iphone

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)];
}

Related

Device Orientation in iphone sdk

i have a requirnmant for ipad to show contents both in landscape and portrait..so i have done the code and done the postions of controlles in each orientations.it works fine..my code is
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
if (interfaceOrientation == UIInterfaceOrientationLandscapeRight ||interfaceOrientation == UIInterfaceOrientationLandscapeLeft ) {
_viewContainer.frame = CGRectMake(342, 1, 681, 707);
_viewtopic.frame = CGRectMake(1, 62, 343, 56);
_viewpublicspeekingheaderleft.frame = CGRectMake(0, 6, 341, 58);
_viewTableview.frame = CGRectMake(1, 118, 341, 590);
_viewFooter.frame = CGRectMake(1, 716, 1024, 48);
if (interfaceOrientation == UIInterfaceOrientationPortrait ||interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown ) {
_viewContainer.frame = CGRectMake(276, 1, 503, 946);
_viewtopic.frame = CGRectMake(0, 58, 275, 50);
_viewpublicspeekingheaderleft.frame = CGRectMake(0, 1, 275, 58);
_viewTableview.frame = CGRectMake(1, 109, 274, 837);
_viewFooter.frame = CGRectMake(1, 954, 767, 48);
}
return YES;
}
the above code works fine for me,i got alla the controllers in corect postions..here is my probm,,i have to set a timer to show full screen and i need to hide some controlers in the viewcontroller,,i had done the timer set to 3 second and after 3 second it hides some controlles and arrange the othe controlers in the view controller to show the ful sceen.it also works fine for me.my code fo this is
- (void)viewDidLoad
{
[super viewDidLoad];
// Define the timer object
NSTimer *timer;
// Create the timer object
timer = [NSTimer scheduledTimerWithTimeInterval:3.0 target:self
selector:#selector(updateorientation:) userInfo:nil repeats:NO];
}
- (void) updateorientation:(NSTimer *)incomingTimer
{
_tableTopic.hidden =YES;
_viewtopic.hidden =YES;
_viewpublicspeekingheaderleft.hidden =YES;
_viewTableview.hidden =YES;
_imgpulbicspeakingabovethetableview.hidden =YES;
_topiclabel.hidden =YES;
_lblpublicspeekingleft.hidden =YES;
UIInterfaceOrientation orientation =[[UIApplication sharedApplication] statusBarOrientation];
if (orientation == UIDeviceOrientationPortrait || orientation == UIDeviceOrientationPortraitUpsideDown ) {
NSLog(#"portrait");// only works after a rotation, not on loading app
_imgMicimage.frame = CGRectMake(69, 130, 635, 216);
_txtContentofTopic.frame = CGRectMake(69, 354, 635, 203);
_viewContainer.frame = CGRectMake(0, 0, 768, 1024);
}
UIInterfaceOrientation orientationLandscape =[[UIApplication sharedApplication] statusBarOrientation];
if (orientationLandscape == UIDeviceOrientationLandscapeLeft || orientationLandscape == UIDeviceOrientationLandscapeRight ) {
NSLog(#"landscape");
_imgMicimage.frame = CGRectMake(93, 113, 836, 239);
_txtContentofTopic.frame = CGRectMake(93, 360, 836, 203);
_viewContainer.frame = CGRectMake(0, 0, 1024, 768);
}
}
it works fine,but after the timer fires ,then i chnage the orientation of the device ,i only get the arrangmnts in the - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { function.i need the arrangmnts in the time function.how to solve this.
thanks in advance.
}
You should not be doing this work in -shouldAutorotateToInterfaceOrientation:. That method is simply to determine whether your UI should rotate (and will soon be superceded by a new system). You should use these methods to lay out your UI in reaction to an actual rotation:
-willRotateToInterfaceOrientation:duration:
-willAnimateRotationToInterfaceOrientation:duration:
-didRotateFromInterfaceOrientation:
Please see the UIViewController docs for more on those.
On iOS 5, -viewWillLayoutSubviews might be another good choice. (It will be called even if your controller's view isn't currently visible.)

iPhone Orientation change frame of view

I want to set position of UILable programatically depend on orientation. But When I get back to Portrait, it doesn't work.
Here is my code :
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
if([[UIApplication sharedApplication] statusBarOrientation] == UIInterfaceOrientationPortrait || [[UIApplication sharedApplication] statusBarOrientation] == UIInterfaceOrientationPortraitUpsideDown)
{
lblUserName.frame = CGRectMake(20, 200, 280, 44);
lblPassword.frame = CGRectMake(20, 246, 280, 44);
}
else
{
lblUserName.frame = CGRectMake(20, 220, 280, 44);
lblPassword.frame = CGRectMake(20, 266, 280, 44);
}
}
There are 2 possibilities.Either you can set the label programatically.And check the condition for orientation in the - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation{}
method.Otherwise you can use auto resizing mask.
try with this IF condition:
if ( UIInterfaceOrientationIsPortrait(toInterfaceOrientation))
{
}
else {}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
//write code here
return YES;
}
- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
{
//write code here
}
Use this two methods for set your label when rotate device.

Only having one view autorotate in xcode?

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.

What is difference between self.view.frame and self.superview.frame and how to use them?

I would like to know that what is difference between these both lines of code?
self.view.frame = CGRectMake(0, 0, 320, 480);
self.view.superview.frame = CGRectMake(0, 0, 800, 900);
and I want to change the view frame when my orientation will change, because it will change the position of labels, and I want them in middle of screen, can anyone guide me ?
I am using following delegate method, for orientation, but it is not working with
self.view.frame
but it is working ok with following line
self.view.superview.frame
See the following code
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations
//return (interfaceOrientation == UIInterfaceOrientationPortrait);
if (interfaceOrientation == UIInterfaceOrientationLandscapeLeft) {
NSLog(#"LEFT");
self.view.frame = CGRectMake(100, 0, 480, 320);
NSLog(#"Show self.view.frame: %#", NSStringFromCGRect(self.view.frame));
// self.view.superview.frame = CGRectMake(-50, -70, 800, 900);
[button setFrame:CGRectMake(340, 320, 100, 30)];
}
if (interfaceOrientation == UIInterfaceOrientationLandscapeRight) {
NSLog(#"RIGHT");
self.view.frame = CGRectMake(0, 0, 320, 480);
NSLog(#"Show self.view.frame: %#", NSStringFromCGRect(self.view.frame));
//self.view.superview.frame = CGRectMake(10, 90, 800, 900); //It is working if I will uncomment it
[button setFrame:CGRectMake(250, 300, 100, 30)];
}
if (interfaceOrientation == UIInterfaceOrientationPortrait || interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown) {
self.view.frame = CGRectMake(0, 0, 320, 480);
//self.view.superview.frame = CGRectMake(0, 0, 800, 900);//It is working if I will uncomment it
[button setFrame:CGRectMake(250, 400, 100, 30)];
}
return YES;
}
self.view is the view of self (if we are talking about viewControllers).
self.view.superview is the view that is holding self.view.
So, in short, if you add a view to the window the superview of that view will be the window.
Setting the frame will fail if the autoresize mask isn't set correctly.
As a generic statement, the first line is trying set the frame of the current viewController's view that this code is written in.
The second line is trying to set the frame of the parent view of the view of the current viewController's view.
What this exactly means, and which one you should be using I'm afraid depends on the view hierarchy you have set up in your application.

How to set the different image view frame size in Orientations in iPhone?

I have created the the image view and set the images in that view and added the image view as sub view of the scroll view(like slide show images). SO i want to set the image view frame size and scroll view frame size as dynamically. I want to set the different image view frame size and the scroll view size in the portrait and landscape mode. So how can i set the different frame size in the landscape and portrait mode.
Here my sample code,
- (void)viewDidLoad {
[super viewDidLoad];
self.scroll = [[UIScrollView alloc] init];
self.scroll.delegate = self;
self.scroll.pagingEnabled = YES;
self.scroll.contentMode = UIViewContentModeScaleAspectFit;
self.scroll.autoresizingMask = ( UIViewAutoresizingFlexibleHeight |
UIViewAutoresizingFlexibleWidth );
[self.view addSubview:self.scroll];
totalArray = [[NSMutableArray alloc] initWithObjects:[UIImage imageNamed:#"dashboard_demo_2.png"],
[UIImage imageNamed:#"dashboard_demo_1.png"],
[UIImage imageNamed:#"dashboard_demo_2.png"],
[UIImage imageNamed:#"3.jpg"],
[UIImage imageNamed:#"4.jpg"],
[UIImage imageNamed:#"12.jpg"],
[UIImage imageNamed:#"5.jpg"],nil];
x = 0;
for(int i = 0; i< [totalArray count]; i++)
{
img = [[UIImageView alloc] initWithFrame:CGRectMake(x, 0, 320, 200)];
img.image =[totalArray objectAtIndex:i];
x = x+320; // in portait mode if the image sets correctly, but landscape mode it will x = x + 480;
[self.scroll addSubview:img];
}
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];
if (orientation == UIDeviceOrientationPortrait ||
orientation == UIDeviceOrientationPortraitUpsideDown )
{
[self portraitMode];
}
else if (orientation == UIDeviceOrientationLandscapeLeft ||
orientation == UIDeviceOrientationLandscapeRight )
{
[self LandscapeMode];
}
return YES;
}
-(void) portraitMode
{
//How to set the frame sizes.
self.scroll.frame = CGRectMake(0, 0, 320, 200);
img.frame = CGRectMake(x, 0, 320, 200);
scroll.contentSize = CGSizeMake(x, 200); // it doesn't set
}
-(void) LandscapeMode
{
//How to set the frame sizes
self.scroll.frame = CGRectMake(0, 0, 480, 320);
img.frame = CGRectMake(x, 0, 480, 200);
scroll.contentSize = CGSizeMake(x, 320); // it doesn't set
}
PLease help me out.
Thanks.
Hi ,
you can register to notification center for change in screen orientation.
Code to enroll with notification center.
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(ScreenRotate:)
name:UIDeviceOrientationDidChangeNotification
object:nil];
Implement the ScreenRotate method.
-(void) screenRotate:(NSNotificationCenter*) notification
{
UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];
if( orientation == UIDeviceOrientationLandscapeLeft)
{
}
else if ( orientation == UIDeviceOrientationLandscapeRight )
{
}
else if ( orientation == UIDeviceOrientationPortrait )
{
}
}
Code to unregister with notification center.
[[UIDevice currentDevice] endGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter] removeObserver:self
name:UIDeviceOrientationDidChangeNotification
object:nil];
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return YES;
}