ios 自定义0.5的线条UIView
来源:互联网 发布:知乎 宋慈 编辑:程序博客网 时间:2024/05/01 17:41
这是本人第一次写有关ios的博客,也不知道要写什么内容,所以先把自己随手弄的一个自定义View给搞上来,大家觉得好用的话也可以拿去用,后期我再优化一下,感觉在布局那块还是蛮好用的,当然,如果你们这群大神有更好的用法请通知我,我刚涉及ios不久。
在ios xib布局文件中,用自动布局约束的时候,UIView的宽和高最小只能设置1,而不能设置0.5之类的,然后在运行的时候会发现线条太粗,影响整体美观。
一般出现这种问题的时候我都会选择自己定义一条线条,然后就在项目上通用,如果是代码生成的UIView当线条的话,自然可以生成0.5宽高的线条,但是在自动布局约束的时候就没办法了,所以我基于约束布局的情况下定义的线条。
这里最重要的是在代码实现xib布局中的约束条件,针对约束这一块,我到时候会另外总结一篇文章出来,然后下面我就直接贴代码了,可以直接用,还封装得不是很好。具体我今天优化了一下,把上一次遗留的三个问题都解决了,然后用起来更加方便了
LineView.h
//// LineView.h// cafe//// Created by xihao on 17/1/13.// Copyright © 2017年 yidont. All rights reserved.//#import <UIKit/UIKit.h>@interface LineView : UIView//@property (nonatomic) BOOL isVertical;@property (strong,nonatomic)IBOutlet NSString* key_direction;@property (strong,nonatomic)IBOutlet UIColor * key_color;//设置线条颜色-(void)setLineColor:(UIColor*)color;@end
LineView.m
//// LineView.m// cafe//// Created by xihao on 17/1/13.// Copyright © 2017年 yidont. All rights reserved.//#import "LineView.h"#define LINE_COLOR [UIColor colorWithRed:220/255.0f green:220/255.0f blue:220/255.0f alpha:1.0] //线条颜色@implementation LineView{ BOOL isVertical; UIView* lineV;}-(void)awakeFromNib{ [super awakeFromNib]; [self initView];}-(void)initView{ NSLog(@"线条方向==%@",self.key_direction); CGRect frame=self.frame; NSLog(@"line_h=%i,w=%i,ww=%i",(int)frame.size.height,(int)frame.size.width,(int)SCREEN_W); NSArray* array=self.constraints; for(NSLayoutConstraint* constraint in array){ if (constraint.firstAttribute == NSLayoutAttributeHeight) { isVertical=false; } if (constraint.firstAttribute == NSLayoutAttributeWidth) { isVertical=true; } } lineV=[[UIView alloc]init]; lineV.translatesAutoresizingMaskIntoConstraints = NO; [self addSubview:lineV]; if (isVertical) { NSLayoutConstraint *top=[NSLayoutConstraint constraintWithItem:lineV attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeTop multiplier:1 constant:0]; top.active=YES; NSLayoutConstraint *bottom=[NSLayoutConstraint constraintWithItem:lineV attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeBottom multiplier:1 constant:0]; bottom.active=YES; NSLayoutConstraint *width=[NSLayoutConstraint constraintWithItem:lineV attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeWidth multiplier:1 constant:0.5f]; width.active=YES; if ([self.key_direction isEqualToString:@"left"]) { NSLayoutConstraint *leading=[NSLayoutConstraint constraintWithItem:lineV attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeLeading multiplier:1 constant:0]; leading.active=YES; }else if([self.key_direction isEqualToString:@"right"]){ NSLayoutConstraint *trailing=[NSLayoutConstraint constraintWithItem:lineV attribute:NSLayoutAttributeTrailing relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeTrailing multiplier:1 constant:0]; trailing.active=YES; }else{ NSLayoutConstraint *centerX=[NSLayoutConstraint constraintWithItem:lineV attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeCenterX multiplier:1 constant:0]; centerX.active=YES; } }else{ NSLayoutConstraint *leading=[NSLayoutConstraint constraintWithItem:lineV attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeLeading multiplier:1 constant:0]; leading.active=YES; NSLayoutConstraint *trailing=[NSLayoutConstraint constraintWithItem:lineV attribute:NSLayoutAttributeTrailing relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeTrailing multiplier:1 constant:0]; trailing.active=YES; NSLayoutConstraint *height=[NSLayoutConstraint constraintWithItem:lineV attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeHeight multiplier:1 constant:0.5f]; height.active=YES; if ([self.key_direction isEqualToString:@"top"]) { NSLayoutConstraint *top=[NSLayoutConstraint constraintWithItem:lineV attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeTop multiplier:1 constant:0]; top.active=YES; }else if([self.key_direction isEqualToString:@"bottom"]){ NSLayoutConstraint *bottom=[NSLayoutConstraint constraintWithItem:lineV attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeBottom multiplier:1 constant:0]; bottom.active=YES; }else{ NSLayoutConstraint *centerY=[NSLayoutConstraint constraintWithItem:lineV attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeCenterY multiplier:1 constant:0]; centerY.active=YES; } } if (self.key_color!=nil) { [lineV setBackgroundColor:self.key_color]; }else{ [lineV setBackgroundColor:LINE_COLOR]; }}-(void)setLineColor:(UIColor *)color{ [lineV setBackgroundColor:color];}@end
0 0
- ios 自定义0.5的线条UIView
- 自定义Cell的线条
- iOS 自定义下拉线条动画
- ios--UIView的基本属性和自定义
- 【iOS开发】自定义UIView边框的颜色
- ios自定义UIView
- ios 自定义uiview
- iOS中标准的自定义控件(UIView的封装)
- iOS使用xib自定义uiview
- iOS使用xib自定义uiview
- iOS使用xib自定义UIView
- IOS 自定义 UIView 实现重用
- [iOS 使用xib自定义UIView]
- ios自定义类(UIView)代码生成简单的UITableViewCell
- iOS UIView自定义四个边角的圆角
- 使用自定义的UIView
- IOS绘制线条、矩形的一般用法
- iOS隐藏UITableView下面多余的线条
- wordpress中文路径出现404错误的解决办法
- c语言程序设计入门(二)
- QT5 一个问题
- reboot与shutdown -r now 区别与联系
- 【matlab】 隐函数作图函数 ezplot
- ios 自定义0.5的线条UIView
- IOS开发入门(11)-导航控制器(1)
- USACO5.1凸包参考代码
- tcpdump抓包
- HTML5——框架
- QT做的推箱子游戏
- Hibernate同名不同包下的映射文件同时加载问题
- 广度优先搜索——填涂颜色
- lnmp nginx 开启pathinfo