Lir*_*rik 12 uitableview ios retina-display
我想知道绘制单点线的最佳方法是什么?我的目标是在tableViewCell中绘制这一行,使其看起来就像本机单元格分隔符.我不想使用原生分隔符,因为我想制作不同的颜色和不同的位置(不是底部..).
起初我使用1px UIView并将其涂成灰色.但在Retina显示器中它看起来像2px.还尝试使用此方法:
- (void)drawLine:(CGPoint)startPoint endPoint:(CGPoint)endPoint inColor:(UIColor *)color {
CGMutablePathRef straightLinePath = CGPathCreateMutable();
CGPathMoveToPoint(straightLinePath, NULL, startPoint.x, startPoint.y);
CGPathAddLineToPoint(straightLinePath, NULL, endPoint.x, endPoint.y);
CAShapeLayer *shapeLayer = [CAShapeLayer layer];
shapeLayer.path = straightLinePath;
UIColor *fillColor = color;
shapeLayer.fillColor = fillColor.CGColor;
UIColor *strokeColor = color;
shapeLayer.strokeColor = strokeColor.CGColor;
shapeLayer.lineWidth = 0.5f;
shapeLayer.fillRule = kCAFillRuleNonZero;
[self.layer addSublayer:shapeLayer];
}
Run Code Online (Sandbox Code Playgroud)
由于某种原因,它在60%的时间里工作..它有什么问题吗?无论如何,我很高兴听到更好的方式.
谢谢.
Rém*_*rin 17
我对一个UIView
类别做了同样的事情.这是我的方法:
#define SEPARATOR_HEIGHT 0.5
- (void)addSeparatorLinesWithColor:(UIColor *)color
{
[self addSeparatorLinesWithColor:color edgeInset:UIEdgeInsetsZero];
}
- (void)addSeparatorLinesWithColor:(UIColor *)color edgeInset:(UIEdgeInsets)edgeInset
{
UIView *topSeparatorView = [[UIView alloc] initWithFrame:CGRectMake(edgeInset.left, - SEPARATOR_HEIGHT, self.frame.size.width - edgeInset.left - edgeInset.right, SEPARATOR_HEIGHT)];
[topSeparatorView setBackgroundColor:color];
[self addSubview:topSeparatorView];
UIView *separatorView = [[UIView alloc] initWithFrame:CGRectMake(edgeInset.left, self.frame.size.height + SEPARATOR_HEIGHT, self.frame.size.width - edgeInset.left - edgeInset.right, SEPARATOR_HEIGHT)];
[separatorView setBackgroundColor:color];
[self addSubview:separatorView];
}
Run Code Online (Sandbox Code Playgroud)
只是为了增加Rémy的绝佳答案,这样做可能更简单.上课UILine.m
@interface UILine:UIView
@end
@implementation UILine
-(id)awakeFromNib
{
// careful, contentScaleFactor does NOT WORK in storyboard during initWithCoder.
// example, float sortaPixel = 1.0/self.contentScaleFactor ... does not work.
// instead, use mainScreen scale which works perfectly:
float sortaPixel = 1.0/[UIScreen mainScreen].scale;
UIView *topSeparatorView = [[UIView alloc] initWithFrame:
CGRectMake(0, 0, self.frame.size.width, sortaPixel)];
topSeparatorView.userInteractionEnabled = NO;
[topSeparatorView setBackgroundColor:self.backgroundColor];
[self addSubview:topSeparatorView];
self.backgroundColor = [UIColor clearColor];
self.userInteractionEnabled = NO;
}
@end
Run Code Online (Sandbox Code Playgroud)
在IB中,放入UIView,单击身份检查器并将该类重命名为UILine.在IB中设置所需的宽度.将高度设置为1或2像素 - 这样您就可以在IB中看到它.在IB中设置所需的背景颜色.当您运行应用程序时,它将成为1像素线,即该颜色的宽度.(你可能不应该受到storyboard/xib中任何默认自动调整大小设置的影响,我无法让它破坏.)你已经完成了.
注意:您可能会想"为什么不在awakeFromNib中的代码中重新调整UIView?" 在故事板应用程序中加载视图大小是有问题的 - 请参阅此处的许多问题!
有趣的gotchya:很可能你只是在故事板上制作UIView,比如10或20像素高,这样你就可以看到它.当然它在应用程序中消失了,你得到了漂亮的一个像素线.但!一定要记住self.userInteractionEnabled = NO
,否则它可能会超过你的其他按钮!
2016解决方案! /sf/answers/2433659721/
归档时间: |
|
查看次数: |
6500 次 |
最近记录: |