I have an UIImageView with content mode Aspect Fill and it's clipping its subviews.
Now I want to add a button as subview and it should protrude at the edges about 20px.
The problem is that the option "clip subview" in interface builder cuts the subviews (as expected).
Is there a possibility to get what I want? Any ideas? Thanks, with kind regards, Julian
Just in case anyone else stumbles across this as I did. I found my solution to be setting clipsToBounds on the image view to FALSE:
[_myImageView addSubview:_myButton];
_myImageView.clipsToBounds = NO;
Now my button overlaps the edges of the image view.
Add the UIButton to the UIImageView's superview and it won't be clipped.
Related
I pinned a UIImageView to 0 in all four directions, but it stays the same and doesn't pin to the edges, how do I fix this?
I bet it has something to do with your content mode of the UIImageView. Try changing it in your storyboard or in code and find a style that suits your need.
I think you can to resolve it by Using Reset to suggested constraints Easily.How about dragging UIImageView edges to main UI edges and then reset to suggested constraints?
I selected Aspect Fill under View > Mode for my UIImageView in my XIB but it stubbornly shows the same sized image.
I I
I I
I MY IMAGE HERE I
I I
I I
i.e. there is blank space above and below MY IMAGE.
What could I be doing wrong?
==== UPDATE
With a red backgroundColor:
RRRRRRRRRRRRRRRRRRRRR
RRRRRRRRRRRRRRRRRRRRR
I MY IMAGE HERE I
RRRRRRRRRRRRRRRRRRRRR
RRRRRRRRRRRRRRRRRRRRR
Shouldn't Aspect Fill make it more like the entire rectangle is filled by something like MAG where MY I and E HERE are off-screen.
Doh. Turned out to be something stupid but I'm posting it here in case it saves someone hours of debugging.
Turned out I had AspectFill in the XIB and then AspectFit in my code (which trumped the XIB).
Have you specified the UIImageView to fill the screen?
If you specfify the mode you change the way the actual image will be displayed inside the imageView. But if your UIImageView does not fill the entire screen the embedded image will too not fill the screen neither.
You should specify the layout constraints to align the imageView with the bounds of its superView.
A easy way to test this is setting the backgroundColor of the UIImageView to something different than transparent - that way you can see where the image and where the imageView are
Another way to help debug this is to add a border. My issue was the image that design provided actually had a clear border around it. Chances are you're sizing it right and there's something wrong with the image itself.
I was experiencing a very similar problem only with a horizontally scrolling UIScrollView.
I had constraints on the scrollview and the UIImageView(s) loaded into the scrollview did not aspect fill properly.
Upon removing the constraints on the scrollview, .scaleAspectFill worked as it should.
I have placed UIImageView in storyboard view. At the center position. I am not using autolayout.
When I run the app. The UIimage origin is set at position (0,0) by default.
I need to position it where I set it in storyboard. It is getting updated automatically.
Please help me to solve the issue?
Thanks
I'm not sure, if I understood your problem right... but if the UIImageView isn't positioned at the location you'd expected, you should check your autoresizing constraints in storyboard.
I am using Auto layout and it's driving me crazy. I have done everything I could to prevent UIImageView from stretching all over. I have no idea why it does that. I tried using constraints, solving auto layout issues. For now, the only solution was turning the auto layout itself.
Could anyone please tell me why xcode is completely ignoring my constraints? I would expect the UIImage to stay as 320x320 when I explicitly set it as that, but noooooooo....
I want to post images, but the site won't let me, and I would like to add code to this question so that it is more specific, but there's literally no code at all. I just dragged "UIImageView" from storyboard, made it small, set constraints as is, and it's just ignoring my code.
You'll want to make sure your UIImageView is set to clip to bounds. If not, the image will spill out of the view and will appear to not stretch correctly (even though in reality it is).
In interface builder, make sure the Clip Subviews box is checked.
If you're not using interface builder, the following code will do the same thing:
yourImageView.clipsToBounds = YES;
With autolayout, the values in the Size Inspector are pretty much ignored.
You might expect that if a number is typed into the boxes on the Size Inspector, Xcode would automatically create constraints to reflect what you typed, but it doesn't. You need to create "Pin" constraints on your own for the height and width of your UIImageView.
Select the image view on your storyboard
Click the "Pin" button at the bottom-right of the storyboard screen
Type the desired size into the "Width" and "Height" fields, and make sure the boxes are checked
Click the "Add 2 constraints" button
When you are done, you should be able to see your constraints in the Size Inspector on the right side of the screen (the purple boxes)
My problem was with the ContentHuggingPriority and ContentCompressionResistancePriority.
The first controls how important it is to keep the view "hugging" the image (staying close to it and not expanding) and the latter controls how resistant to compression the view is.
We use PureLayout, so the code to fix it was:
[NSLayoutConstraint autoSetPriority:ALLayoutPriorityRequired forConstraints:^{
[myImageView autoSetContentCompressionResistancePriorityForAxis:ALAxisHorizontal];
[myImageView autoSetContentHuggingPriorityForAxis:ALAxisHorizontal];
}];
(my problem was only in the horizontal axis)
May 15, 2016: Updated to PureLayout v3 syntax. If you use a version <= 2 just put UIView instead of NSLayoutConstraint. Thanks Rudolf J!
I had the same problem. I added a UIImageView into a prototype cell and wanted to align it next to the text but it kept stretching the image when I applied the constraints. In my case, I changed the 'Mode' property for the Image View to 'Aspect Fit' and it worked fine.
You can try to add UIImageView programmatically.
Call this code in your viewDidLoad method or where you want.
UIImageView *yourImageView = [[UIImageView alloc]initWithFrame:CGRectMake(50.0f, 50.0f, 320.0f, 320.0f)]; // x and y coordinates, width and height of UIImageView
[yourImageView setImage:[UIImage imageNamed:#"your_image.png"]];
[yourImageView setContentMode:UIViewContentModeCenter]; // you can try use another UIViewContentMode
[self.view addSubview:yourImageView];
I have a custom UIview
I need to increase its width and also the width of its subviews like webviews and another label etc. dynamically.
I have used
_view.setframe - xframe;// this increases the width but the content stays where it is
_view setneedsdisplay or needslayout are not working.
Do I use the autoresizing mask before adding the view in controller? and how?
Can someone please tell me how to go about doing this.
Thanks
You need to set autoResizesSubviews on the parent to YES, then set the autoresizingmask for each subview. Here is the documentation for autoresizingmask
https://developer.apple.com/library/ios/#documentation/WindowsViews/Conceptual/ViewPG_iPhoneOS/CreatingViews/CreatingViews.html#//apple_ref/doc/uid/TP40009503-CH5-SW5
This image from apple should help you understand:
Thanks for your inputs
but i worked by recalling
[self layoutSubviews];//and changing the widths