UIImageView overflowing its containing UIView after calling sizeToFit - ios

My UIView (width: 352px) has a UIImageView subview (default width in Storyboard: 312px).
I want the UIImageView to adapt itself to the dimensions of the image it contains with the constraint that the width of the image view shouldn't exceed a maximal width size (in my case, 312px).
I set up the 'autosizing' configuration of my UIImageView to have a fixed left, top and right margin size. Nevertheless, when I call sizeToFit on my UIImageView and its image is larger than 352px, the UIImageView gets wider than its containing UIView.
Is there a convenient method to prevent such a behavior without doing the math based on the image dimensions? Am I using sizeToFit the right way?

Just use setClipsToBounds:
[imageView setClipsToBounds:YES];

Related

How to achieve label embededd in UIImageView in iOS Swift like shown in image?

I want to achieve label/Button embededd in UIImageView in iOS Swift like shown in image.Has anyone done that?
You just need to play with UIView, UILabel And UIImageView.
You view hierarchy will be like below...
- UIView (mainView)
- UIImageView (imageView)
- UILabel
You need to make mainView.clipsToBounds = true so that after giving corner radius to it, it's subview will not go beyond its superview's bounds.
For imageView, you can set its content mode to aspect fill/ aspect fit as per your requirement.
Have a square UIView with clipsToBounds property set to YES which contains UIImageView and UILabel. The image view should cover all the view frame. The label should be positioned at the bottom of the view over the image view. Then just set appropriate corner radius (half of the view's height) for the view's layer and set its masksToBounds property to YES. That's it

UIImageView frame same as scaled UIImage inside it (with Aspect Fit)

I'm using UIImageView with Content Mode set to Aspect Fit. UIImageView resizes correctly my image but it doesn't change own frame after resizing even there is no constraints set.
What I need:
I need UIImageView to resize own frame according image inside. I've configured image view at Storyboard.
I need to layout again other objects at view because UIImageView's size is changed.
What I've tried:
I've created:
UIViewController (white background) → UIImageView (gray background) → sample image inside it.
Constraints: leading & trailing constraints set to zero, Y constraint is set to align UIImageView vertically.
As you see after image scaled correctly UIImageView's frame is still has incorrect size (gray background).
I've tried to set UIImageView size manually:
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
let newSize = AVMakeRect(aspectRatio: backgroundImageView.image!.size, insideRect: backgroundImageView.frame)
backgroundImageView.frame = newSize
}
In that example code I've calculated image size and set frame size to image's size. I don't know if it is better way so I don't need to use AVFoundation's methods.
Problem: other object is place incorrectly because UIImageView's size changed and constraints are invalid now.
That methods doesn't work for me:
self.view.setNeedsLayout()
self.view.layoutIfNeeded()
Even I will call my UIImageView size calculation at viewDidLayoutSubviews() other constraints is still using old UIImageView's size.
Questions
How to change UIImageView's size correctly? Maybe I can do it without code? Maybe I need to play with Hugging/Compression? I've tried something but got no luck.
How to force view to recalculate other constraints so they will use new UIImageView size? For example I can put red square at the top of UIImageView, but after UIImageView has been resized by my method, red square still will be at the old UIImageView's position.

is it possible to add uiview with big frame into uiview with small frame

In my app, I need to add subview in another view but my issue is that height and width of parentView is 50*50 and height of subview is 150*150 so how can I add this? because when I add this view, rest portion of subview is getting cropped.
The edges are bounded in the smaller UIView. Because the UIView is smaller it is going to crop out the larger subview. if you still want the super view to be 50x50 and don't want the larger subview cropped out then turn off setClipsToBound property of the UIView:
Swift: view.setClipsToBound = false
Objective-C: [view setClipsToBound:NO];
Another solution is just to make the subview smaller or make the superview containing the large subview larger.

Placing the button on top right of UIImageView

I have a UIImageView which is set to mode Aspect Fit. Now I want to place an edit button on top right corner of that image view. I am using auto layout and have a constraint of (Button->Trailing Space to superview) and the layout is connected via IBOutlet.
Once the image is set in UIImageView, I want to resize the ImageView Frame, so I can place the UIButton on top right. But UIImageView is not resizing as per image. Look at the gray part in the attached image (It is the background of ImageView)
imageView.clipsToBounds = YES;
imageView.image = image;
[imageView sizeToFit];
buttonLeadingSpace.constant = imageView.frame.origin.x+imageView.frame.size.width-44;
First of all, you should align your button with ImageView top & trailing. you can add constant=padding you need for your button.
UIImage is displaying as per your properties set for UIImageView (ie. contentMode = AspectFit). Here are the few options you might be interested in
If you want to fill the complete width just remove the height constraint from the UIImageView. It will fill the width and increase the UIImageView height as per image aspect ratio.
If you want same height and width of UIImageView set contentMode = aspectFit. this will fill the complete UIImageView but might clip the image.
Remove leading and trailing constraints from UIImageView. Align horizontal center and add image width constraint.

xcode UIView ScaleAspectFit for UITextfield setLeftView

I'd like to have an icon in my textfield and I'm using a the setLeftView attribute.
my code at the moment is
UIView *leftView = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 26, tb_teamA_playerA.bounds.size.height)];
leftView.contentMode = UIViewContentModeScaleAspectFit;
[leftView setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed : #"person_26" ]]];
[tb_teamA_playerA setLeftView :leftView];
[tb_teamA_playerA setLeftViewMode: UITextFieldViewModeAlways];
that's the result.
the very left image is a separate UIImageView with the same image set and the scale set to ScaleAspectFit.
So I was expecting the two images to be the same size and scale but they are not?!
You are using UIViewContentModeScaleAspectFit, so the image will fit the size of the UITextField.
I think you should use UIViewContentModeScaleAspectFill, and if you have some problem with ths size and the two images still don't have the same size, you should resize you image.
The following is from the Doc
UIViewContentModeScaleToFill
Scales the content to fit the size of itself by changing the aspect ratio of the content if necessary.
UIViewContentModeScaleAspectFit
Scales the content to fit the size of the view by maintaining the aspect ratio. Any remaining area of the view’s bounds is transparent.
UIViewContentModeScaleAspectFill
Scales the content to fill the size of the view. Some portion of the content may be clipped to fill the view’s bounds.

Resources