I'm reimplementig some kind of UISplitViewController
. Now I need to draw a separator line between the master and the detail view. Now I have some questions for this:
- Should the separator line be on the view controller itself or should it be a separate view?
- What about Auto Layout? Setting a frame is not allowed.
I saw solutions for CALayer
/CAShapeLayer
, drawRect
(CoreGraphics) or using the background color of an UIView
/UILabel
. The last one should cost too much performance according to the folks there.
On the one side it is comfortable to draw the line in the UITableViewController
itself. Or should a separate UIView
be created? If I embed a separate UIView
there will be much more constraints and it will complicate things (would have to set separate widths) ... Also it should adapt to orientation changes (e.g. the size of the UITableViewController
changes -> the separation line should also resize).
How can I add such a dividing rule? Such a dividing rule can be seen here:
Updating the @mharper answer to Swift 3.x
If you need to add a true one pixel line, don't fool with an image. It's almost impossible. Just use this:
How to use:
Actually in storyboard, simply make a UIView that is in the exact place, and exact width, you want. (Feel free to use constraints/autolayout as normal.)
Make the view say five pixels high, simply so you can see it clearly, while working.
Make the top of the UIView exactly where you want the single-pixel line. Make the UIView the desired color of the line.
Change the class to
UILine
. At run time, it will draw a perfect single-pixel line in the exact location on all devices.(For a vertical line class, simply modify the CGRectMake.)
Hope it helps!
I took @joe-blow's excellent answer a step further and created a view that is not only rendered in IB but also whose line width and line color can be changed via the inspector in IB. Simply add a
UIView
to your storyboard, size appropriately, and change the class toLineView
.New code
This should work for horizontal and vertical lines:
Original answer
Using frames in an Auto Layout project seems not suitable for me. Also I'd need the actual frames after auto layout has been applied and than I'd have to draw another view on top of it. In a fixed layout based on frames it is no problem, but not here. That's why I chose the following apporach for now:
I created a subclass of
UIView
and overwrotedrawRect
like in Draw line in UIView or how do you draw a line programmatically from a view controller?. Here is another option.Because I'm using C# I give the code samples in that programming language. In the links I posted you can get the Objective-C version if you want.
DividerView:
In
viewDidLoad
of my container class I instantiate theDividerView
withDividerView separator = new DividerView ();
.Auto Layout:
Then I'm using Auto Layout to layout it's position (all in
viewDidLoad
):Here the constraints are added:
Result:
This approach seems to work. Now my separator line is overlapping my detail view (only the first point). One could adapt the values or changes the constraints of the master and the detail view so that there is room between those for the separator.
Alternatives:
One could also add a subview to the content view of each
UITableViewCell
. Examples can be found here or here.