UITableview clipsToBounds - iphone

I am creating an iPad view which has a tableview as a subview. The tableview only takes a small portion of the screen and is somewhere near the middle of the screen and it contains some menu items. I want people to be about to scroll this tableview up and down however I do not like how the cells disappear against a hard edge. When I set clipsToBounds to false, I get what I want in that the hard edge is not there anymore but the top/bottom cell disappears when the tableview needs that cell for recycling. Is there a common technique to avoid this hard-edge of when the cells scroll up against the tableview's bounds? I was thinking of adding gradient alpha masks on a parent container view but it seems a bit over the top.

There are no hard and fast rule about this, but you certainly can do whatever you feel best. What I would do in a case of floating tableView is giving it a nice border using layer. It is easy to code (2~3 lines). Round the edge to make it pretty.
If you want to drop shadow, it gets a little more complicated but possible. Just draw a bezier curve path of a rectangle (where you want your shadow to appear). Assign that CALayer shadowPath. Then add it to the table.
You can also gradient an alpha to make it appear shadow like.
But I would suggest, you set clipsToBounds to YES since it looks horrible otherwise, given the fact that the table 'floats' somewhere in your view.

Related

set margin to collection view's cell

I'm beginner with collection view,
I need to create cell like this
Can I do it from Storyboard?
I want to add margin of cell = 4 or 5 in the top, bottom, lift and right in iPhones and iPads of all sizes, or I if need to do that programmatically How I can add the contents of the cell like the image above?
There is no "margin" here. There is simply a rectangle with a shadow, and everything else is drawn in front of it. The simplest solution is probably a custom UIView that draws itself as a rectangle with a shadow. Make that the content view's direct subview, and everything else in the cell is a subview of that. The inset of the rectangle-with-shadow within the cell's content view can be determined by autolayout (and the position of all the stuff inside it can be determined by autolayout too).
Thus it was trivial for me to obtain this sort of thing:
And of course you can tweak the border color, the background color, and so forth.

How to perform an interactive animation with Auto Layout in which you interpolate between different constraints?

I'd like to have an interactive animation where a view transitions from one set of constraints to another as the user drags up/down the screen. The background image should shrink vertically (easy), and the profile image should change from being horizontally and vertically aligned to the background image to being anchored to the top and left corners of the background image. Is this possible?
Yes, it is possible, and you can see this effect in the Avvo app, for example (in the lawyer profile screen). The trick here is to smoothly transition from one set of constraints to another.
Here's an outline of how to do this:
add a UIScrollView. Add the view you want to animate as a subview to the scroll view.
Make your view controller implement
UIScrollViewDelegate
In the delegate method
scrollViewDidScroll(_:), update the constraints' constants as
needed.
Also in that method, when contentOffset crosses a
threshold value, flip to a different set of constraints, by setting
their active property to true or false.
To keep the view (the headshot image, in my example), pinned to the top as you scroll, just continuously update its top spacing constraints based on the contentOffset.y value.
Achieving a perfectly smooth transition may take some trial and error, but it definitely can be done!
Here are the screenshots showing the transition as you scroll up:

Covering space from one UITableView's cell with another one

The design of the UITableView is something like this:
I want to make my cells with a tiny triangle, like in a comic book. The thing is, how can i add the triangle to the size of my custom cell? The common rectangle size wouldn't work if i want the user to be able to tap that little rectangle.
And how can i make the opposite? I want the triangle to cover the space of another cell, so tapping the little triangle of the first cell, covering part of the second cell's rectangle space, would activate de first one. This is, substracting a little triangle from the cell's space.
Not sure it would work, but building on user697562's comment, you could try the following:
Add a small UIView to the table cell to represent the small triangle
Rotate it using its transform property, making sure that, along with its frame, it will have the proper placement.
Add a UITapGestureRecognizer to the UIView
Add an instance variable to the view to save the indexPath of the cell it's in (or even the above cell, since it will be associated with the above cell). This way when the gesture recognizer is triggered, you know what row you're in.
Write the action method associated with the gesture recognizer to do the same thing as tableView:didSelectRowAtIndexPath: would do for the above cell.
Set the separatorStyle property of the UITableView to UISeparatorStyleNone, so that it won't draw the lines between cells. If this doesn't work just set the separatorColor property to your table cell's background color.
Draw a border along the top & bottom of the cell, accounting for the triangle.
Good luck with it! Let me know whether it works if you try it.

How to auto rotate your own custom views?

I've learned that the best way to get graceful rotation is to set the auto rotation mask on the view that you want resize or move. This works fine if you're using SDK views like UILabel, but if you have your own custom view that uses the drawRect method it doesn't rotate as gracefully. In fact the only thing that happens is that it stretches whatever you drew in drawRect.
I've tried redrawing both before and after the rotation, but it doesn't give me that smooth rotation.
I looked at a UITextField auto rotating (flexible width) in slow motion and it follows the edge perfectly during the rotation. That is what I want my view to do, so how do I do that? My views jump to the right position either before or after the rotation.
The following line will make your UIView stretch the middle pixel only. If this is not your desired behavior I suggest you read the documentation for contentStretch to learn how to manipulate the values of the CGRect.
[self setContentStretch:CGRectMake(0.5, 0.5, 0.0, 0.0)];
I would guess that the UITextField you're looking at has at least three subviews, one displaying the left cap of the field's border, one displaying the right cap, and one displaying the middle, with autoresizing masks of "flexible right margin", "flexible left margin", and "flexible width", respectively. If you set up your custom view something like that, and make sure its autoresizesSubviews property is set to YES, then you should get the same smooth resize that the text field does.

Making UITableViewCells bleed over

What I'm trying to achieve might be a bit of a novelty, but maybe someone has already done this or has some great ideas.
Here's the situation: I have a UITableView sitting on top of a UIImageView (which provides the background) that has a brushed metal texture. The fist row in the table is colored black. What I'd like to achieve, is the following: when the user tries to scroll up (by pulling down) from the top of the tableview and thus causes the "bounce" physics to kick in I'd like to have the space at the top of the table view be black to seamlessly blend rather than have the user see the background image.
I can't just add another black subview under the the table because then if the tables contents is just one row it'd show under the first cell as well, considering bounce scroll lets the user scroll halfway down the screen.
I've tried setting the cells background view to a black view with a rect like 0, -100, 320, 142 (where the cell itself should be 42px high) and setting clipsToBounds to NO on both the backgroundView and the cell itself, but no dice.
Any ideas?
Set UITableView.tableHeaderView to a suitable view. Headers/footers are useful in general (you can have shadows at the end like UIWebView, or a background that moves with the cells, or whatever).
Make sure you handle the zero-cells case sensibly (although the problem isn't visible if scrolling's disabled when there aren't enough cells to require it, or if there can't be zero cells).