ios 自定义0.5的线条UIView

news/2024/7/20 23:11:18 标签: ios, uiview, 布局

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


http://www.niftyadmin.cn/n/994407.html

相关文章

使用dubbo服务时spring配置文件报错的解决办法

2019独角兽企业重金招聘Python工程师标准>>> 打开项目后发现*-*-dubbo.xml文件中报错&#xff0c;百思不得其解&#xff0c;如图所示&#xff1a; 于是在网上搜索&#xff0c;结合自己的情况&#xff0c;解决了这个问题。 1、下载dubbo.xsd文件&#xff0c;放在指定…

php学习 运算符 表达式 $php_self php中的网页重定向

运算符 意义$ 变量标志& 变量的指针&#xff08;加在变量前&#xff09; 不显示错误信息&#xff08;加在函数前&#xff09;-> 对象的方法或属性> …

Android Webview场景下防止dns劫持的探索

背景 阿里云HTTPDNS是避免dns劫持的一种有效手段&#xff0c;在许多特殊场景如HTTPS/SNI、okhttp等都有最佳实践&#xff0c;但在webview场景下却一直没完美的解决方案。 拦截方案是目前已知的一种在webview上应用httpdns的可行方案&#xff0c;本文从拦截方案的基本原理出发&a…

对IOCP的一点个人理解

IOCP 理解起来不是那么直观,以多天的研究, 发表一点点个人的见解. GetQueuedCompletionStatus相当于一个总的消息处理机, 若本身程序不对其发出请求信息, 那么它是不会接到任务信息的, 即便有其它的程序对其发送了数据信息, GetQueuedCompletionStatus也不会有信息到达. 信息发…

Android各种函数单位

该博客随时更新。因为每次写函数都会考虑这个单位是什么&#xff0c;所以嫌比较麻烦&#xff0c;在这里总结一下&#xff0c;方便以后使用。 paint.setStrokeWidth() 单位是 px 。 paint.getStrokeWidth()单位是px。 textview.setTextSize() sp转载于:https://www.cnblogs.com…

ios新建项目图解

鄙人新手一个&#xff0c;是由开发Android转向ios开发的&#xff0c;当然也只是想多了解一下ios开发&#xff0c;毕竟技多不压身。 之前自学了oc&#xff0c;然后都是直接在我同事的项目上直接开发功能&#xff0c;但是对于布置一个新的项目来说&#xff0c;我很多都不清楚&am…

OGNL是什么

OGNL表达式是&#xff08;Object-Graph Navigation Language&#xff09;是对象图形化导航语言。OGNL是一个开源的项目&#xff0c;Struts2中默认使用OGNL表达式语言来显示数据。与Serlvet中的EL表达式的作用是一样的。 参考&#xff1a; http://commons.apache.org/proper/com…

ICE专题:反叛之冰 Internet Communications Engine

转自:韩磊CSDN Marc Laukien&#xff1a;Object-Oriented Concepts, Inc的创办人和总裁。开放源码的ORBacus (原名OmniBroker&#xff0c;完全遵从CORBA的ORBA&#xff09;的主要作者。在2001年IONA&#xff08;著名的CORBA技术公司&#xff09;收购OOC之后&#xff0c;他出任I…