這是本人第一次寫有關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