iOS 画分割线的问题

来源:互联网 发布:江恩九方图软件 编辑:程序博客网 时间:2024/06/10 08:31

    很多控件中希望加一条线, 常见横的竖的, 比如在一个页面的header里, cell中, 像tableview那样那样的分割线.

所以我总结了几种方式供大家参考:

   1. 利用CGContext去画, 举个例子:

[objc] view plaincopy
  1. UIImageView *imageView=[[UIImageView alloc] initWithFrame:self.view.frame];    
  2.     [self.view addSubview:imageView];    
  3.      
  4.     self.view.backgroundColor=[UIColor blueColor];    
  5.      
  6.     UIGraphicsBeginImageContext(imageView.frame.size);    
  7.     [imageView.image drawInRect:CGRectMake(00, imageView.frame.size.width, imageView.frame.size.height)];    
  8.     CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);    
  9.     CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 15.0);  //线宽  
  10.     CGContextSetAllowsAntialiasing(UIGraphicsGetCurrentContext(), YES);    
  11.     CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 1.00.00.01.0);  //颜色    
  12.     CGContextBeginPath(UIGraphicsGetCurrentContext());    
  13.     CGContextMoveToPoint(UIGraphicsGetCurrentContext(), 100100);  //起点坐标  
  14.     CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), 200100);   //终点坐标  
  15.     CGContextStrokePath(UIGraphicsGetCurrentContext());    
  16.     imageView.image=UIGraphicsGetImageFromCurrentImageContext();    
  17.     UIGraphicsEndImageContext();  

2. 利用UIView, 我看到过有同学直接设置UIView宽或高为1来做线条:

[objc] view plaincopy
  1. UIView *line_view = [[UIView alloc] initWithFrame:CGRectMake(00320.f1.f)];  
  2.     [line_view setBackgroundColor:[UIColor redColor]];  
  3.     [self.view addSubview:line_view];  

这样就是一条红色的横线lol


3. 利用UIImageView:

[objc] view plaincopy
  1. UIImage *separatorImage = [UIImage imageWithRenderColor:[UIColor redColor] renderSize:CGSizeMake(320.f1)];  
  2.         UIImageView *topSeparatorView = [[UIImageView alloc] initWithImage:separatorImage];  
  3.         topSeparatorView.center = CGPointMake(320.f*0.50.5);  
  4.         [self addSubview:topSeparatorView];  
这其实就是加入一张图片.


上述就是总结的三种横线画法, 当然还有其他很多方式, 有想法的可以留言.

0 0
原创粉丝点击