How to make custom UIView properties disappear as soon as they exit the boundaries of the view? - ios

I have a custom UIView that has a few properties like UIButtons and UITextFields. I am animating these to go left and right using UIView.animate(withDuration: 0.25) {} and am simply changing the constraints in the closure to move the properties to their respective positions. These positions are often outside of the view, because I want them to disappear. Is there someway that I can keep the animations, but once the properties begin cross the boundary of the custom UIView, they disappear instead of showing up outside the UIView?

It would be clearer if you called them subviews.
Anyway, based on your comment, it sounds like you want to set the superview's clipsToBounds to true. Then any portion of a subview that is outside it's superview's bounds will not be visible.
You can set clipsToBounds programmatically, or in the view's Attributes inspector in your xib or storyboard.

Related

Placing a button at the bottom of a collectionView

I'm trying to make a collectionView that shows 10 item at first, when scrolling to the bottom I want to have a button that clicking it will load more info, its something simple but the thing is that I'm not using storyboard and I did everything programmatically, so how it can be done?
UICollectionView is a subclass of UIScrollView. So you need to:
Add your button as subview of your UICollectionView
Set it's frame in the way you prefer (if you count your frames in code - just set it manually) based on .contentSize of you UICollectionView.
Set .contentInset.bottom property based on the height of your button/footer
You need to handle the case when .contentSize changes. If your button should be visible all the time, maybe better to set your button frame in viewDidLayoutSubviews() function
If you want to show/hide it in some cases, you can animate it using UIView.animate(withDuration:) by setting .contentInset.bottom in the block

masksToBounds property of UITableViewCell's content view not working

I have a UITableView with a prototype cell in it. The cell has a content view property which (I assume) refers to the content view which is in the cell by default. I've added a UIImageView into this content view (for a background image) and I've set the clipsToBounds property of this image view to true to make sure it doesn't show past the borders of its parent view. In addition to this, I've also set the masksToBounds property of the parent view (the content view of the table view cell) to true, which to my knowledge should be hiding its subviews outside the borders. However, if I set the cornerRadius of the content view to 45, the background image view inside the content view is still visible outside the borders. I've triple-checked to make sure all the properties are pointing to the right views and everything seems to be okay, but for some reason the content view of the table view cell doesn't want to mask to its bounds. Does anyone have any experience with this? If so, how do I deal with this?
contentView of the UITableViewCell is also changed internally before being displayed. I recommend you to make clipsToBounds in willDisplayCell method of the UITableViewDelegate, or put the same sized (constrained, autoresized) UIView on top of the contentView, setting contentView.backgroudColor = UIColor.clear, and moving all contentView's subviews to the view. Hope this helps, good luck!

Hide non child UIView in UIView

I want to hide a non child UIView in a UIView but I don't know how to do it.
To be specific, I have a UITableView. Each UITableViewCell has another view inside it (a wrapper view called wrapperView). The wrapper has some labels set up in IB and some created programmatically. I have created a custom slide mechanism that reveals buttons under the wrapper (like the standard one does).
The labels created programmatically don't exceed wrapper's bounds because it clips the subviews. The problem is with the labels created in IB. They are the subviews of contentView.
How can this be solved? Is there a way for a UIView to clip other views on the same level(not parents nor children)? Or "transfer" the labels to the wrapper view?
It isn't completely clear what you're asking. A view will only clip it's subviews, not views that happen to fall within their frame rectangle but aren't subviews.
If you want to move a view object from one view hierarchy to another you can use addSubview(_:) to do so. That will automatically remove it from it's current parent view. To quote the Apple docs:
Views can have only one superview. If view already has a superview and
that view is not the receiver, this method removes the previous
superview before making the receiver its new superview.

Animating UIView with auto layout when the view is just created and about to appear

I have a UIView that contains subviews that contains subviews...(Hierarchy of UIViews).
Every UIView is set with auto layout. When I press a button, this UIView is created and then I would like it to drop from the top of the screen.
The problem when I use layoutIfNeeded in an animation is that if will animate everything at the same time as it is a newly created UIView (not yet display).
My question is, is there a way to do the animation of only the UIView dropping with all the subviews already laid out in it?
I guess you can do it by creating the UIView when UIViewController displays and then hide it but I was wondering it there was another way.
Cheers
If you call layoutIfNeeded right after you add it to a view, while still outside the view, it should lay out it's views correctly. And then in the animation you call it again so the view animate to the final position.

Rotations in iOS if UIViewAutoresizing doesn't cut it

I'm creating a view controller that has non-trivial rotation behavior. I can't find a way to get all my transformations to occur with only UIViewAutoresizing (I'm doing things like keeping by bottom bar at the same "absolute value" on the screen" while rotating buttons). What's the correct way to handle these type of rotation behaviors? Is there a way to disable auto resizing for a specific view and handle the rotations yourself?
If you set a view's autoresizesSubviews property to NO, that view will not apply autoresizing to its subviews. You can do this in a xib or storyboard by unchecking the “Autoresizes Subviews” checkbox in the attributes inspector.
The correct place to programmatically lay out subviews is in the layoutSubviews method of your custom UIView subclass. Your view will receive the layoutSubviews method whenever its size changes or when its subviews array changes. You will receive it when the view is first being put on the screen, and inside the autorotation animation block.

Resources