UIImageView not clipping to UIView bounds while animating - ios

I have a UIImageView in a UICollectionViewCell that I am moving in relation to a scroll views content offset. This is all to provide a parallax effect when the user scrolls horizontally, however there seems to be a clipping issue when the scroll view begins to scroll.
Instead of clipping, the edges persist and are shown outside of the containers boundaries.
The attached screenshot best showcases the behavior...
In the screen shot the scroll view has begun scrolling right and the accompanying views have begun translating however the UIImageView has not clipped its borders to match the receiver. Which in this case should be the transparent UIView.
Here is the UIImageView
let cellImage: UIImageView = {
let c = UIImageView()
c.clipsToBounds = true
c.layer.masksToBounds = true
c.contentMode = .scaleAspectFill
c.image = UIImage(named: "tree")
c.translatesAutoresizingMaskIntoConstraints = false
c.layer.cornerRadius = 15
return c
}()
The UIImageView is then added to a containerView and is set to fill it's bounds.
cellContainerView.addSubview(cellImage)
//add constraints
My thoughts are that if anything this issue may have to do with my interpretation of what the .clipsToBounds property does.
The documentation states:
Setting this value to true causes subviews to be clipped to the bounds
of the receiver.
I initially thought the receiver was the view you attached the UIImageView to however that is looking to not be the case.
Any suggestions?

As the documentation states:
Setting this value to true causes subviews to be clipped to the bounds
of the receiver.
Currently you are setting this property on cellImage ,so in this case it is the receiver, so its subviews will be clipped. But you want to clip this imageview so I think you need to set this property on cellContainerView, since you want its subviews to be clipped.

Related

Textview bounds UIImageView out of the View

I'm trying to rebuild the twitter post tweet feature as well as the UI. I'm currently at the point that, when I type something long in the UITextView. My selected image that I want to tweet get bounds out of the container view that is in a scrollview.
See this example:
The blue background is the scrollview background and the grey background is the container views background. I can't scroll too.
My UIImageView has initially 0 height and is directly attached to the top of the textview.
I change the height of the UIImageView with their constraint:
// sets the image
let newHeight = self.postedImageView.getImageHeight(forImage: image)
self.postedImageViewHeightConstraint.constant = newHeight
self.view.layoutIfNeeded()
self.postedImageView.showImage(image)
How can I make this the scrollviews contentView height to grow as I type something in the textview so it doesn't get bounds out the view?
These are my constraints:
Update:
With the postedimageviews bottom constraint added to the content views bottom

On tableView Update - Image will extend ImageView bounds

I am trying to build a UITableView which cells are containing a UIImageView and a few UILabels.
Not in general but sometimes updating the list via reloadData().
My table images conflict with UIViewContentMode.ScaleToFill and clipping to bounds.
The cell will appear like this, extending it's edges.:
These options are set for the image in awakeFromNib:
self.miniImg.layer.cornerRadius = self.miniImg.frame.width/2 // Circular
self.miniImg.clipsToBounds = true
self.miniImg.layer.masksToBounds = true
self.miniImg.contentMode = UIViewContentMode.ScaleToFill
My idea was to try and fix this by clipsToBounds and masksToBounds. But it didn't work.

Can't scroll UIImageView with AspectFill in UIScrollView and AutoLayout

I have a UIScrollView with a single UIImageView child. The scrollView is pinned on all sides to the (root) parent container with autolayout, and the child imageview is also pinned to all sides with content mode set to AspectFill.
UIImage *image = [UIImage imageNamed:#"photo1.jpeg"];
_imageView.image = image;
_imageView.contentMode = UIViewContentModeScaleAspectFill;
_scrollView.contentOffset = CGPointZero;
_scrollView.contentSize = _imageView.image.size;
_scrollView.zoomScale = 1;
The image in this case is wider than my screen. When I launch my app, the imageview is correctly displaying the image filling up the screen. However, I can't seem to pan the image left or right. When i zoom in, I can pan, however, I'm unable to pan to the corners of the image.
I feel like my content size is not being computed properly thanks to auto-layout, but I'm not sure which parameters I can play which would allow me to scroll the image to its edges.
I've attached an image below, where you can see that the image can't be scrolled in a way that displays the beginning of 'happiness'. I also uploaded the sample here
the child imageview is also pinned to all sides
Instead, turn off auto layout for the child image view (set its translates... to YES). You are already setting the scroll view's contentSize to the image size, so now scrollability will leap into life and will interface correctly with zooming.

Why UIImageView's image can extend beyond itself?

Previously, i thought UIImageView's image cannot extend beyond the view. However, recently i read the book Programming in IOS8, it says:
You should also pay attention to a UIImageView’s clipsToBounds property; if it is false, its image, even if it is larger than the image view and even if it is not scaled down by the contentMode, may be displayed in its entirety, extending beyond the image view itself.
And then i did some simple test, that's true.
Here is the question: What's the purpose for Apple to design UIImageView with this feature? I, for now, cannot find the advantage of it. I think this makes contentMode a fake property.
Here is an example:
It is a refreshView with customized pull-to-refresh animation above in a UITableView. I set up a UIImageView with the same size as the refreshView and add it as a background view. Relevant code here:
let imgView = UIImageView()
imgView.frame = self.bounds
imgView.image = UIImage(named: "refresh-view-bg.png")
imgView.contentMode = .ScaleAspectFill
imgView.clipsToBounds = true
self.addSubview(imgView)
It works fine:
However, when i annotate this line:
imgView.clipsToBounds = true
It turns like that:
The image extend beyond the UIImageView
ps: I'm not using autoLayout in this demo
This is due to autolayout. UIImageView returns its -intrinsicContentSize by the calculation of image size. If you want to avoid that you should set a width (or height) constraints, or play with content hugging/compression resistance priorities if you want to share or not the space when you view is laid out with other views around.

UIView is a child of UIScrollview, but when I zoom in or out the UIView doesn't stay in the correct location relative to the image in the scrollview

The scrollview shows a floorplan, and the UIView is a little green dot that shows the user's location on the floorplan. So I need to be able to zoom in, and have the green dot stay in the same spot on the floorplan. When I pan, the green dot moves correctly, but when I zoom, it moves away from where it should be.
Here is a screenshot of my storyboard. The small green square is the view that is a child of the Scrollview, and shows the user's location. I move it around programatically, but this is not causing the problem. http://i.imgur.com/TTRMXNS.png
Heres the code that puts the tilemap into the scrollview:
CCDirector* director = (CCDirector *)[self childViewControllers][0];
director.view.frame = CGRectMake(0, 0, floorplanWidth, floorplanHeight);
CGSize sizeOfScene = CGSizeMake(floorplanWidth, floorplanHeight);
float minimumScale = 1;//This is the minimum scale, set it to whatever you want. 1.0 = default
_sceneScrollView.maximumZoomScale = 4.0;
_sceneScrollView.minimumZoomScale = minimumScale;
_sceneScrollView.zoomScale = .1;
[_sceneScrollView setContentMode:UIViewContentModeScaleAspectFit];
[_sceneScrollView sizeToFit];
[_sceneScrollView setContentSize:sizeOfScene];
[_sceneScrollView addSubview:[director view]];
There's actually a lot of things wrong with what I see above that leads me to believe that you should probably look up more information regarding iOS and scrollviews. The first thing is that a scrollView has a contentSize that's either intrinsically determined via auto layout, or via code by setting the contentSize. In this case, your contentSize would be set via setting the image. The second thing to note is when you zoom in using a scrollView, you provide a view to zoom in on. This view and it's respective subviews will be zoomed in on. I see that your green dot is in the storyboard and is a direct subview inside the scrollview's contentView, this is incorrect. Your green dot will need to be either a subview of your own containerView with the UIImageView and your dot in it, or a subview of your UIImageView (which is sometimes wanky in the storyboard, you'll you'll want to add that in code). To note, UIImageViews have their own scale factor inside of them versus iOS's scale factor. So if you're attempting to dynamically mark anything in a UIImageView, keep that in mind.

Resources