为了账号安全,请及时绑定邮箱和手机立即绑定

UIView底边框?

UIView底边框?

iOS
MMTTMM 2019-11-22 15:25:07
UIScrollView *toScrollView我要添加一个(它是屏幕的宽度)的灰色底边框(与iPhone的本地Messages应用程序的compose视图的to-field的边框完全一样)。为此,我遵循了可可触摸:如何更改UIView的边框颜色和厚度?并使用自定义设置覆盖了顶部边框,UINavigationBar并将其toScrollViewx坐标-1和宽度322设置为使左右边框刚好不在屏幕上。看起来不错,但这有点不合时宜,我想知道是否有更好的方法可以做到这一点。- (void)viewDidLoad {    [super viewDidLoad];    // Add UINavigationBar *navigationBar at top.    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]                                             initWithBarButtonSystemItem:UIBarButtonSystemItemCancel                                             target:self action:@selector(cancelAction)];    UINavigationBar *navigationBar = [[UINavigationBar alloc]                                      initWithFrame:CGRectMake(0.0f, 0.0f, 320.0f, 44.0f)];    navigationBar.items = [NSArray arrayWithObject:self.navigationItem];    // Add UIScrollView *toScrollView below navigationBar.    UIScrollView *toScrollView = [[UIScrollView alloc]                                  initWithFrame:CGRectMake(-1.0f, 43.0f, 322.0f, 45.0f)];    toScrollView.backgroundColor = [UIColor whiteColor];    toScrollView.layer.borderColor = [UIColor colorWithWhite:0.8f alpha:1.0f].CGColor;    toScrollView.layer.borderWidth = 1.0f;    [self.view addSubview:toScrollView];    [self.view addSubview:navigationBar]; // covers top of toScrollView}
查看完整描述

3 回答

?
慕森卡

TA贡献1806条经验 获得超8个赞

代替UIView@ImreKelényi建议使用,可以使用CALayer:


// Add a bottomBorder.

CALayer *bottomBorder = [CALayer layer];


bottomBorder.frame = CGRectMake(0.0f, 43.0f, toScrollView.frame.size.width, 1.0f);


bottomBorder.backgroundColor = [UIColor colorWithWhite:0.8f 

                                                 alpha:1.0f].CGColor;


[toScrollView.layer addSublayer:bottomBorder];


查看完整回答
反对 回复 2019-11-22
?
忽然笑

TA贡献1806条经验 获得超5个赞

这是更通用的Swift扩展,可为任何子UIView类创建边框:


import UIKit


extension UIView {      

  func addTopBorderWithColor(color: UIColor, width: CGFloat) {

    let border = CALayer()

    border.backgroundColor = color.CGColor

    border.frame = CGRectMake(0, 0, self.frame.size.width, width)

    self.layer.addSublayer(border)

  }


  func addRightBorderWithColor(color: UIColor, width: CGFloat) {

    let border = CALayer()

    border.backgroundColor = color.CGColor

    border.frame = CGRectMake(self.frame.size.width - width, 0, width, self.frame.size.height)

    self.layer.addSublayer(border)

  }


  func addBottomBorderWithColor(color: UIColor, width: CGFloat) {

    let border = CALayer()

    border.backgroundColor = color.CGColor

    border.frame = CGRectMake(0, self.frame.size.height - width, self.frame.size.width, width)

    self.layer.addSublayer(border)

  }


  func addLeftBorderWithColor(color: UIColor, width: CGFloat) {

    let border = CALayer()

    border.backgroundColor = color.CGColor

    border.frame = CGRectMake(0, 0, width, self.frame.size.height)

    self.layer.addSublayer(border)

  }

}

迅捷3

extension UIView {

    func addTopBorderWithColor(color: UIColor, width: CGFloat) {

        let border = CALayer()

        border.backgroundColor = color.cgColor

        border.frame = CGRect(x: 0, y: 0, width: self.frame.size.width, height: width)

        self.layer.addSublayer(border)

    }


    func addRightBorderWithColor(color: UIColor, width: CGFloat) {

        let border = CALayer()

        border.backgroundColor = color.cgColor

        border.frame = CGRect(x: self.frame.size.width - width, y: 0, width: width, height: self.frame.size.height)

        self.layer.addSublayer(border)

    }


    func addBottomBorderWithColor(color: UIColor, width: CGFloat) {

        let border = CALayer()

        border.backgroundColor = color.cgColor

        border.frame = CGRect(x: 0, y: self.frame.size.height - width, width: self.frame.size.width, height: width)

        self.layer.addSublayer(border)

    }


    func addLeftBorderWithColor(color: UIColor, width: CGFloat) {

        let border = CALayer()

        border.backgroundColor = color.cgColor

        border.frame = CGRect(x: 0, y: 0, width: width, height: self.frame.size.height)

        self.layer.addSublayer(border)

    }

}


查看完整回答
反对 回复 2019-11-22
?
梦里花落0921

TA贡献1772条经验 获得超6个赞

在以下类别中实施:


UIButton + Border.h:


@interface UIButton (Border)


- (void)addBottomBorderWithColor: (UIColor *) color andWidth:(CGFloat) borderWidth;


- (void)addLeftBorderWithColor: (UIColor *) color andWidth:(CGFloat) borderWidth;


- (void)addRightBorderWithColor: (UIColor *) color andWidth:(CGFloat) borderWidth;


- (void)addTopBorderWithColor: (UIColor *) color andWidth:(CGFloat) borderWidth;


@end

UIButton + Border.m:


@implementation UIButton (Border)


- (void)addTopBorderWithColor:(UIColor *)color andWidth:(CGFloat) borderWidth {

    CALayer *border = [CALayer layer];

    border.backgroundColor = color.CGColor;


    border.frame = CGRectMake(0, 0, self.frame.size.width, borderWidth);

    [self.layer addSublayer:border];

}


- (void)addBottomBorderWithColor:(UIColor *)color andWidth:(CGFloat) borderWidth {

    CALayer *border = [CALayer layer];

    border.backgroundColor = color.CGColor;


    border.frame = CGRectMake(0, self.frame.size.height - borderWidth, self.frame.size.width, borderWidth);

    [self.layer addSublayer:border];

}


- (void)addLeftBorderWithColor:(UIColor *)color andWidth:(CGFloat) borderWidth {

    CALayer *border = [CALayer layer];

    border.backgroundColor = color.CGColor;


    border.frame = CGRectMake(0, 0, borderWidth, self.frame.size.height);

    [self.layer addSublayer:border];

}


- (void)addRightBorderWithColor:(UIColor *)color andWidth:(CGFloat) borderWidth {

    CALayer *border = [CALayer layer];

    border.backgroundColor = color.CGColor;


    border.frame = CGRectMake(self.frame.size.width - borderWidth, 0, borderWidth, self.frame.size.height);

    [self.layer addSublayer:border];

}


@end


查看完整回答
反对 回复 2019-11-22
  • 3 回答
  • 0 关注
  • 874 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信