How to auto-resize UIView proportionally? - iphone

I have a UIView whose height should always be exactly 1.5 times the width. When the view is auto-resized (in this case, because the iPhone is rotated), this proportion gets messed up. How can I make sure the height/width ratio doesn't change as the view resizes?

You need to set the views autoresizingMask property to UIViewAutoresizingNone. This will prevent the size of the view from changing at all when the parent view's size changes (such as when the phone rotates.) If you want the view to resize on rotation, but maintain it's aspect ratio (for example if you want it wider in landscape but still 1:1.5), then you will need to set the view's frame yourself to the desired dimensions (but maintaining the desired ratio) in the view controller's willRotateToInterfaceOrientation:duration: method.

You could implement the -sizeThatFits: method for your view to keep the size in proportion.

Alternatively, you could use a custom superview that implements - (void)layoutSubviews to have complete control over the layout.

Related

How to use UIScrollView in IB? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
steps for creating UIScrollView with Interface Builder
I am using UIScrollView for one of my view. I have added view first and then scroll view, then some controls over scroll view, all are added through IB. Every things works fine until the page is fit, some controls need to be added down that is exceeding the UIView length, I am not sure, how to add controls in IB in that case, So I have added programmatically, but my scrollview is not scrolling down, it is scrolling upto my view height, I have tried setting the new view height, scrollview's frame and content size nothing works, Can anyone please suggest me what to do now...
i) how to add control in IB more than its view length.(view height are disabled).
ii) How to make my scrollview which covers full view.
Create the view for the UIScrollView separately.
Technically, UIScrollView can hold controls directly in it. But that is not the point of it. You should always (if using IB) design the view separately and then place it inside the UIScrollView and set the contentSize property to the size of that view.
So, instead of putting contents directly in the UIScrollView in the main view, drag a separate view from the IB objects library on the nib (not on the main view).
Set the width of this view to the width of UIScrollView. Start placing controls on it and extend the height of view. For every new control to be placed, the height must be increased so as to hold that control. You should not place control outside a view from IB. (Although you can).
After designing the view, drag the view inside the UIScrollView and set the Y value of this view to 0. (When you drag a view inside UIScrollView it seems to upset it's Y value).
Now programmatically set the contentSize of UIScrollView to the size of this view. For this you'll need to create an IBOutlet for the UIView you just created.
myscrollview.contentSize = myview.frame.size;
And you're done.
-EDIT-
it is so simple. Follow the steps I'm giving you below you will get what you want.
decide the actual size that is the size in which you want your scroll view to display. say it is 100 * 5o.
set your UIView size also 100 * 50.
Now increase the scrollview hieght in nib.
add what you want
Now check upto what size you have reached.
Suppose it is reached upto 300
So set your contentsize 100 * 300 or 100 * 310 programmatically
In nib set scrollview size 100 * 50
Enjoy programming.
It's very simple, first remember your UIScrollView Frame and then just change UIScrollView frame's Y-position by subtracting 100 (e.g 0 to -100) and add same to height(460 to 560). Then simply add you controls at your required position by continuing this steps...
For eg:
ScrollView Frame (0,0,320,460)
after CHANGE:
(0,-100,320,560)
After finishing with controls adding simply set previous frame like:
(0,0,320,460)

iOS - reversed height/width with orientation

I'm creating a custom container view controller but when I want to resize the frame of one of the child viewcontroller's view its size is not good regarding the orientation of the screen.
To be more precise in the init method of my container I retain 2 instance variable (2 viewcontrollers). Then in the viewdidload method of my container, I want to resize the frame of the view of one viewcontroller and add the 2 as subviews.
My problem is that I'm working in landscape orientation only and when I ask the frame size of one of the viewcontrollers, the height and the width are reversed. I should have a width of 1024 and a height of 748 but I get a width of 748 and a height of 1024 !
Do you know why ?
PS : in all the viewcontrollers including the container viewcontroller I set the shouldautorotate method to UIOrientationInterfaceIsLandscape()
Thanks a lot
I'm not sure of your exact setup, but here's some general info that may help:
When you have view controllers very near the top of the view hierarchy (or indeed at the top) you can find that you get this 'swapping' effect of the width and height. The swapping usually manifests itself on the frame, but not on the bounds of the view. This is because bounds is effectively some transformations applied to the frame -- and sometimes these transformations include a 90 degree rotation (due to device being in landscape mode).
Note that the exact timing of when you check the frame property can be important too. If you're checking the property after the view loaded but before it has appeared on-screen, you can get the 'wrong' result.

Scaling a sub classed UIView

I have created a subclass of UIView and am trying to scale and move the view from within its m file but am running into some problems. When I used the command:
self.frame = CGRectMake(self.frame.origin.x-10,self.frame.origin.y-10,self.frame.size.width/2,self.frame.size.height/2); the object moves location but does not resize (the view only contains a few UIImageViews). In the xib file of the sub class I have the options checked to Clip Subviews and to Autoresize Subviews but neither appears to happen. Any ideas as to why the view will not resize with this command and how I could get it to resize.
Resizing your view is not the same as scaling it. Think of your view as a picture frame. What you're doing above is moving the frame, and also moving the lower right corner (you're shortening the frame's wood bars) - but that does not automatically shrink the picture.
There are four ways of resizing a view with subviews:
Let the superview clips its subviews (by setting view.clipsToBounds = YES): Subviews do not resize or relayout, but only show in the area that is inside the frame.
Let the superview not clip its subviews (by setting view.clipsToBounds = NO): Changing superview size does not have any visual effect on subviews, they also show outside of the frame.
Give the subviews autoresizingMasks: The subviews do not change size, but they relayout according to their autoresizing mask (for instance, a subview may always stay 10 px off the lower right corner of the frame, or may always span exactly the width of the frame.) Note that this does not necessarily automatically scale subview content. Set subview.contentMode accordingly.
Scale the superview: By setting superview.transform = CGAffineTransformScale(superview.transform, 0.5, 0.5), you shrink the superview and all its subviews (you essentially zoom out). Note that this makes superview.frame undefined, which means you shouldn't use that anymore. It can also make things a bit blurry.
You could also "manually" change all the subviews, but that kind of defeats the purpose of having a nice view hierarchy.
As MishieMoo said, temporarily set the backgroundColor of your superview to something visible. This will very likely show you that your view is indeed changing.

Dynamically set UIView size depending on height of grouped table view

Setup: I have a UIView with a scroll view nested within it. Within the scroll view I have a label, uiimage, and a tableview (grouped). The label, uiimage, and tableveiw are populated by a webservice. The last section in the grouped table view contains text that will never be the same and could be as long as 5 characters to 250+. Also, this view is pushed from a basic tableview (so it has a navigation bar. If that matters at all).
Expected: The uiview should extend in height depending on the height tableview extends to. Then I will be able to set the scrollview to accommodate the height I need to be able to scroll.
Problem: I'm not quite sure how to approach the issue. I really only know how to change the height to fixed values, which will not work properly in almost any scenario.
Am I using UIScrollView incorrectly? Do I need to resize the UIView at all?
You don't have to modify your UIView frame size, which has to be the size of your screen. The UIScrollView frame size must also be the same, it represents the part of its view actually displayed.
What must change is the UIScrollView contentSize, which defines height and width for data inside it ;)
You can calculate it using each inside element's height and by adding the correct margin.
Thus, you could have a UIScrollView content size of 320 * 600, which will let you automatically scroll down.
In fact, you have to display your content independently of the final frame size. If you have a content of 500*500, just display it inside your UIScrollView. Then tell it the size of it's content, and it will automatically set scrolling possibilities if needed.
Turns out I had to create a UIView programmatically and set it as the header of the UITableView. It works perfectly now. :)

UIScrollView autoresizing content?

If I set UIScrollView height to self.view.frame.size.height and setAutoresizingMask:UIViewAutoresizingFlexibleHeight then when the view loads the UITabBarController and the UINavigationBar the scroll view frame resizes to suit.
However the content size (set at: self.view.frame.size.height) doesn't change. Fair enough I guess - i can't seem to find an autoresizing option for that, so why would it autoresize.
My fix is to just set the content size as less than the frame height, thus the content will always be the height of the frame. (eg: set content size height to 10points).
I'm just wondering if this is the best way to go about this, or if there's a better way? I'm doing this code in viewDidLoad.
Thanks
Tom
The UIScrollView's contentSize property define the size of the view displayed for Span&Zoom. It normally does not have any relation with the container (i.e. scrollview) size but if you really need to resize the content according to the scrollview size, you can overload the layoutSubViews method to do so.