UIImageView fit to View frame size - objective-c

When I create a new ViewController and I drag a UIImageView to this, the UIIV grows to fit fullscreen with the width and height from the screen. If I change between 4 and 3,5" screen, the UIImageView changes his height dynamically.
BUT, when I try to insert an UIImageView to a view with more elements, this UIImageView is setted with 4:3 ratio size, and if I try to resize this to fullscreen, when I change between screen sizes, a part of the imageView is hidden.
How can I fix it?

Short Answer: use Auto layout
Long Answer:
select imageview, add left, right, top, bottom constraints (all equal to 0) to tableview
select imageview, update frames

Related

UIButton inside UIView goes out of bounds

I'm struggling with a problem that probably it's really stupid.
I've created an UIView and inside the UIView I've placed an UIButton and a UISwitch.
The UIView scales it's width based on the screen size keeping from left and right 45px, the UISwitch keeps it's frame size and the UIButton has its frame size changed based on the UIView frame size.
The problem is that the UIButton's text goes out of bounds for no reasons, this is the result I get:
The Blue background is the one of the UIButton and the Yellow background is the one of the UIView
I'm not able to align the text of the UIButton on top, this is what I've tried:
[_privacyButton setTitleEdgeInsets:UIEdgeInsetsMake(0,0,0,0)];
_privacyButton.titleLabel.baselineAdjustment = UIBaselineAdjustmentNone;
And also from the Storyboard I've set the vertical aligment top
Please someone help me, this UIButton is driving me crazy
You need to set constrains for the particular unbutton, i am unable to see you uiswitch, where you place it?.

How do I resize a UIView and force its subviews to resize

I have a UIScrollView with a UIView as a subview. The UIView has a bunch of data entry fields arranged vertically - essentially just a fixed format data entry Form.
I want to keep the UIView's vertical size and adjust its horizontal size to match the size of the UIScrollView which changes depending on the orientation of the device. Note that this is all placed in the Detail view of a UISplitViewController.
So the user will have to scroll vertically but not horizontally as all the text fields on the UIView should resize themselves to fit horizontally on the screen.
Currently if I resize the UIView by changing the frame width to match the UIScrollView's frame width then the UIView subviews (the text fields) don't resize themselves according to the constraints setup in IB. The UIView just seems to get clipped. There is no horizontal scrolling so this aspect is working correctly.
I have autoresize subviews set on UIView and on UIScrollView.
Any tips on what to do here ? Also where would I put code to resize the UIView if the device orientation changes ?
Additional information.
I created the UIView in IB as a separate view in the same NIB as the DetailViewController containing the UIScrollView. Because it is much taller than the UIScrollView the only way I can find for creating it in IB is if I set it up as a separate view of the desired width and height. I then create an IBOutlet and add this view as a subview to UIScrollView in the viewDidLoad method. This all seems to work find with the views all displaying correctly, with the exception that the UIView subviews are not resized horizontally.
Any suggestions on what I may be doing wrong here?
Since you are using not putting the view inside scrollview directly from the xib, the IB doesn't provide the options to give constraints that has anything to do with superview. You might have to add the constraints programatically.See here.
EDIT:
Also try using the below on the view (haven't tried, should work according to documentation, but not sure with auto-layout):
self.view.autoresizesSubviews = YES;
Or if you don't want to use auto layout at all then the earlier method of setting the view to expand (horizontal/vertical) in the size inspector would do. For this you have to disable auto-layout. Select xib-> File Inspector -> Uncheck auto-layout checkbox
OK after more discovery I think I have found the right way to do what I am trying to do so i thought I should leave a note regarding this. Remember I am trying to create a scrollable form that resizes its width but not its height so the user only has to scroll up and down to access fields.
To create a large fixed size form that requires scrolling on the device make sure you set the ViewController size to Freeform in IB. Then you can create the view to be whatever size you want in IB and at runtime it will resize to the devices size.
Place the UIScrollView (I call it the scrollView) in the main view and pin it left and right and top and bottom (i.e set constraints using IB)
Place a UIView (I call it the contentView) in scrollView and make it the same size as the scrollView and also pin it on all sides to its superview (the scrollView)
Now add all the labels and text fields are required to the contentView and make sure you add vertical constraints from top to bottom and left to right so that autolayout can figure out the width and height in order to calculate the scrollView.contentSize
Set the contentView width constraint as a fixed size to make it look reasonable in IB. Bear in mind we want the width of contentView to always match the scrollView width so the user does not have to scroll sideways, only up and down. We will set the proper width in code at runtime as I have not found any way of doing this in IB only. Perhaps setting priorities on constraints might achieve this but I think UIScrollView won't do this for you.
Now add a property the ViewController.h file and connect this to the contentView width constrain you created in 5) above.
#property (weak, nonatomic) IBOutlet NSLayoutConstraint *contentViewWidth;
Finally create a viewDidLayoutSubviews method in ViewController and add the following code to set the contentView width to be the same as the scrollView width.
- (void)viewDidLayoutSubviews
{
self.contentViewWidth.constant = self.scrollView.frame.size.width;
[self.view layoutSubviews];
}
If things don't resize properly check all your constraints are correctly set. IB seems to do some things that seem strange to me. But finally I have it working with what appears to be minimum coding.
You can also resize vertically in the same way as long as you set the constraint priority on subviews in contentView to be lower than 1,000. Also set a greater than or equal to size with a high priority if you don't want it smaller than a certain size.
If anyone can figure out how to set the contentView such that it resizes its width to match the scrollView using only IB constraints I would love to know how.

Autoresize Height and Width Proportionally Interface Builder

I have a UIImageView that is set to autoresize it's height based on the height of the Superview, i.e. when the In Call Status bar comes down. How do I make the entire view resize proportionally so that the width of the UIImageView changes when the height changes?
I would like to do this in Interface Builder, but programmatically can be used as well.
Thanks
The easiest way in iOS 6 is to use the (new in iOS 6) autolayout feature. It is very easy to make a view's width always be a fixed proportion of its height.
Otherwise you'll have to detect the change in your view controller's layoutSubviews and use code to resize the UIImageView.
However, consider the alternative of letting image resize rather than the whole image view. If you give the UIImageView the right contentMode, it will automatically resize the image proportionally if the view's height changes.

How to make a UITextview and a UIImageView scrollable

I have put an UIImageView and a UITextView in a UIScrollView. The result is that when I scroll, the text scrolls but the image stays on the same place.
Is it possible to make the UIImageView and UITextView to scroll as a whole? Can this be done using the storyboard?
The reason that you are only scrolling the text is that the UITextView has its own UIScrollView and you are only scrolling the UITextViews internal scroll view.
To achieve what you are looking for you need to:
Disable scrolling in the UITextView
Make the UITextView big enough to cover all the text
Make the UIScrollViews contentSize big enough to cover both the UIImageView and the UITextView
Layout the UIImageView and the UITextView in the UIScrollView
Disable scrolling in the UITextView
Set scrollingEnabled to false on your textview, this can be done directly in the storyboard.
Resize the UITextView
The text view could cover any length of text and without knowing what text is being set, you can't set the correct size. I.e, you will need to set the size in code when the text is set in the UITextView.
Resize the contentSize
See above (if you don't know the height of the UITextView you can't know the content size).
Layout the UIImageView and the UITextView in the UIScrollView
You can however layout the UIImageView and the UITextView in the storyboard and only change the bounds of the text view and the contentSize of the scrollView when the text is set.
View hierarchy
The image below shows how your view hierarchy should look when you are finished. The frame of the scrollView is blue. Its content is red and is bigger than the frame. Within lies the image view (yellow) and the text view (green).

How do I resize a UIScrollView so that it's not 100% of the parent UIView?

I've built an app using the UITabBar template. I have a few tabbar items, one item displays a view. That view has a UIScrollView element that has paging enabled to mimic the behaviour of the iPhone springboard i.e. pages that can be scrolled left to right.
I'm trying to drop in a UIPageControl, so I've resize the UIScrollView so that it's slightly shorter than the parent UIView height and have placed a UIPageControl below it.
When I run the app the UIScrollView is always 100% of the height of the parent UIView and I can't see the UIPageControl.
I've got the following code in my viewDidLoad method of the view controller for the tab:
UIScrollView *tempScrollView=(UIScrollView *)self.view;
tempScrollView.contentSize=CGSizeMake(640,377);
This sets the content size ok and I can scroll left to right. I've tried adding:
tempScrollView.frame=CGRectMake(0, 0, 640, 377);
To to resize the scroll view but it still shows 100%. See diagram below showing the issue:
I think you shouldn't resize the frame to 640, 377 because it would make the paging stop working, once the contentSize would be the same as the frame size.
One solution would be to set the desired frame size in the interface builder (like the left most figure) and set proper autosizing masks. I gues what you are looking for is the configuration below
To check if the changes are working, I would use a uiscrollview of height visibly smaller, just to make sure the behaviour is the desired.
If you want your view to scroll you need to change the tempScrollView.contentSize to be bigger than tempScrollView.frame
If you do this:
CGFloat contentWidth = tempScrollView.frame.size.width*2;
tempScrollView.contentSize=CGSizeMake(contentWidth,377);
You will have 2 pages.
You need to activate paging too with:
[tempScrollView setPagingEnabled:TRUE];