这是本人第一次写有关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