I have all my subviews set up so that they are based on self.view.
EG: UIImageView *image = [[UIImageView alloc] initWithFrame:CGRectMake(0,0,(self.view.frame.size.width-20),(self.view.frame.size.height-90))];
however when the view rotates (shouldRotateToDeviceOrientation or whatever) the views all stay the same size. How can I make them change shape to fit? Can I do this automatically?
Thanks
Absolutely. Take a look at the autoresizingMask property. If you set your image view, in this case, to have an autoresizing mask of UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight, then when its parent view resizes (as it may or may not automatically do when your app rotates—you might have to set a similar autoresizingMask on the parent view), it'll maintain the exterior margins you set up for it.
Just in case you have a view in the bottom of parent view this should help:
self.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin;
Where self is your child view, that should be resized. If you do not set UIViewAutoresizingFlexibleTopMargin, then in horizontal mode you will not see your view, if it was in the bottom in vertical mode.
Also do not forget to set:
self.autoresizesSubviews = YES;
And autoresizing modes for all child view in your view. So that when it is resized, everything inside it also gets resized.
you have to [view setAutoresizingMask:...]
Related
I'm adding a subview to the UiView of my mainviewcontroller, that is presented in a similar way as a UIModalViewController with the Formsheet-style. (so it doesn't fill teh whole screen)
No if the device rotates the subview somehow gets resized to fill the whole mainview...
Even if I manually set:
subViewCtrl.view.autoresizingMask = UIViewAutoresizingNone;
it still autoresizes.
Why can't it simple stay in the middle as any other subview would?
Ok, I figured it out myself.
I accidentally did
[self.view.superview addSubview:rowCtrl.view];
so I changed this too
[self.view addSubview:rowCtrl.view];
and it worked as intended. Thanks anyway!
Of course, then one also has to set
rowCtrl.view.autoresizingMask = UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleBottomMargin;
otherwise it doesn't get centered properly when the interface rotates.
Is the view resizing, or is the view staying the same size while the dimensions of the screen change? You may want UIViewAutoresizingFlexibleWidth and/or UIViewResizingFlexibleHeight. If you use those values then your subview will resize to remain a constant distance from the edges of the screen; your view will change sizes, but remain centered.
I By using this command
-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
I can rotate my view automatically ,Is there any way to rotate and ellabarate and fit with screen.
Thanks in advance,
(Deepan)
You can use the autoresizing properties of your views to make them resize to fit the screen.
For example, if you want a view to resize horizontally and vertically, you can use:
myView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
To control whether a view will automatically resize its subviews, you use the boolean autoresizesSubviews property.
To control how a container view will layout its subviews when the container view's size changes, you use the contentMode property.
For more information, refer to Apple's UIView Class Reference.
In my app all my controls are created in code. I have not used IB for controls. now i want to rotate the view to landscape mode. I know I have to use the shouldAutorotate method for this purpose.
But since I have not used IB, how can I resize the controls when they are in landscape mode? How can I position them correctly using code only?
In most cases you can get views to resize themselves appropriately just by setting their autoresizingMask property to some combination of:
UIViewAutoresizingFlexibleLeftMargin
UIViewAutoresizingFlexibleRightMargin
UIViewAutoresizingFlexibleTopMargin
UIViewAutoresizingFlexibleBottomMargin
UIViewAutoresizingFlexibleWidth
UIViewAutoresizingFlexibleHeight
For example, let's say you want a view's width to increase when you rotate it to landscape, you want it to maintain the same margins relative to the top, left, and right sides of the screen, and you want its height to remain the same. This means that out of the six attributes above, only the width and bottom margin should be flexible. The other four attributes are fixed. So you would do:
yourView.autoresizingMask = UIViewAutoResizingFlexibleWidth | UIViewAutoresizingFlexibleBottomMargin;
In those rare cases when you can't get the views to behave appropriately using their autoresizingMask property, you can wrap them in a custom view and override that view's layoutSubviews method. This method gets called when the view's frame changes due to autorotation, so you can check [[UIApplication sharedApplication] statusBarOrientation] and update the frames of your subviews manually.
You position them correctly using code the same way you likely positioned them in the original orientation, for example by setting their frame.
As for where in code to do this, check out the various orientation change methods of UIViewController that will be called when the device orientation changes. You could, for example, move/resize the controls in the view within willAnimateRotationToInterfaceOrientation:duration:.
Is there a "stick-to-bottom" autoresizing mode in Cocoa-Touch? Basically, I got a UIImageView in the lower part of another UIView. When the UIView resizes, I don't want to change the UIImageView's size, but keep it in the lower part of the UIView, while only resizing the other subviews in my UIView above the UIImageView.
Is that easily feasible?
Assuming you want to have the image view resize horizontally:
imageView.autoresizingMask = UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleWidth;
I have uiimageviews as subviews for uiscrollview. I made my uiviewcontroller resize the scrollview contentsize and offset similar to how it was done here: http://github.com/andreyvit/ScrollingMadness/tree/master. The only difference is that I don't want my image views to take up the whole screen but try to stretch proportionally so I used
scrollView.contentMode = UIViewContentModeScaleAspectFit;
scrollView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
view.contentMode = UIViewContentModeScaleAspectFit;
view.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
and a few other modifications. Now, the images are expanding proportionally and it looks fine without a nav bar but if I push the ScrollingMadnessController, it'll create a nav bar which will cause vertical scrolling.Is it even possible to disable the vertical scrolling?
As far as I know, vertical scrolling is disabled if the height of the scroll view's contentSize or the content in the scroll view does not exceed the frame's height of your scroll view. In other words, if this is the case there is no reason to allow for vertical scrolling (the same is true for horizontal scrolling obviously).
If you enable vertical bouncing, however, you still might get the impression that vertical scrolling is enabled, but you are not really scrolling content in this case.