画一条分隔线与自动相结合的布局 [英] Draw a separator line in conjunction with Auto Layout

查看:149
本文介绍了画一条分隔线与自动相结合的布局的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我reimplementig某种 UISplitViewController 的。现在我需要绘制主详细视图之间的分隔线。现在,我有一些问题要问这样的:

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:


  • 如果分隔线是视图控制器本身还是应该是一个独立的看法?

  • 怎么样自动布局?设置框架是不允许的。

我看到了的CALayer / CAShapeLayer 的drawRect (CoreGraphics中)或使用的的UIView / 的UILabel 的背景颜色。最后一个应根据乡亲那里要花太多的性能。

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.

在一方面它是舒适绘制在的UITableViewController 本身就行了。还是应该单独的UIView 创建?如果我嵌入一个单独的的UIView 会有更多的限制,这将复杂的事情(必须单独设置宽度)......此外,它应该适应取向的变化(如的的UITableViewController 的大小变化 - >分离线也应该调整)

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:

推荐答案

如果您需要添加一个真正的一个像素行后,不要用图像愚弄。这几乎是不可能的。只需使用这样的:

If you need to add a true one pixel line, don't fool with an image. It's almost impossible. Just use this:

@interface UILine : UIView
@end

@implementation UILine

-(void)awakeFromNib
    {
    float sortaPixel = 1.0/[UIScreen mainScreen].scale;

    // recall, the following...
    // float sortaPixel = 1.0/self.contentScaleFactor;
    // ...does NOT work when loading from storyboard!

    UIView *line = [[UIView alloc] initWithFrame:
    CGRectMake(0, 0, self.frame.size.width, sortaPixel)];

    line.userInteractionEnabled = NO;
    line.backgroundColor = self.backgroundColor;

    line.autoresizingMask = UIViewAutoresizingFlexibleWidth;

    [self addSubview:line];

    self.backgroundColor = [UIColor clearColor];
    self.userInteractionEnabled = NO;
    }

@end

如何使用:

其实在故事板,只是让一个UIView是在确切地点,然后准确的宽度,你想。 (随意使用的限制/自动布局是正常的。)

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.)

请视图5个像素高,只要这样你就可以清楚地看到它,而工作。

Make the view say five pixels high, simply so you can see it clearly, while working.

您要单像素线使的UIView的完全吻合。使UIView的行所需的颜色

Make the top of the UIView exactly where you want the single-pixel line. Make the UIView the desired color of the line.

更改类 UILine 在运行时,将划出一道完美的单像素线所有设备上的准确位置。

Change the class to UILine. At run time, it will draw a perfect single-pixel line in the exact location on all devices.

(对于一个垂直线类,只需修改CGRectMake。)

(For a vertical line class, simply modify the CGRectMake.)

希望它帮助!

这篇关于画一条分隔线与自动相结合的布局的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆