对于UIScrollView *toScrollView(这是屏幕的宽度),我想添加一个灰色的底部边框(与iPhone的原生消息应用程序的撰写视图的to-field完全相同).
为了做到这一点,我跟随Cocoa Touch:如何改变UIView的边框颜色和厚度?并使用自定义覆盖顶部边框UINavigationBar并制作了toScrollViewx坐标-1和宽度322,以便左右边框就在屏幕外.
这看起来很好,但它有点像黑客,我想知道是否有更好的方法来做到这一点.
- (void)viewDidLoad {
[super viewDidLoad];
// Add UINavigationBar *navigationBar at top.
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemCancel
target:self action:@selector(cancelAction)];
UINavigationBar *navigationBar = [[UINavigationBar alloc]
initWithFrame:CGRectMake(0.0f, 0.0f, 320.0f, 44.0f)];
navigationBar.items = [NSArray arrayWithObject:self.navigationItem];
// Add UIScrollView *toScrollView below navigationBar.
UIScrollView *toScrollView = [[UIScrollView alloc]
initWithFrame:CGRectMake(-1.0f, 43.0f, 322.0f, 45.0f)];
toScrollView.backgroundColor = [UIColor whiteColor];
toScrollView.layer.borderColor = [UIColor colorWithWhite:0.8f alpha:1.0f].CGColor;
toScrollView.layer.borderWidth = 1.0f;
[self.view addSubview:toScrollView];
[self.view addSubview:navigationBar]; // covers top of toScrollView …Run Code Online (Sandbox Code Playgroud) 我需要在UIView中绘制一条水平线.什么是最简单的方法.例如,我想在y-coord = 200处绘制黑色水平线.
我不使用Interface Builder.
我有一个UIViewController.我想在其中一个以编程方式创建的视图中绘制一条线.看起来很简单,但我还没有找到有效的示例代码.
我想知道绘制单点线的最佳方法是什么?我的目标是在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%的时间里工作..它有什么问题吗?无论如何,我很高兴听到更好的方式.
谢谢.
我想在UITableView中只设置右边框?
到目前为止,我已经尝试过以下代码.但它是在整个表中设置边界,因为它在mac中,但我想实现它应该只设置右边界: -
self.tableView.layer.borderWidth = 1.0;
Run Code Online (Sandbox Code Playgroud)