UIView底边框?

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

}


MMTTMM
浏览 875回答 3
3回答

慕森卡

代替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];

忽然笑

这是更通用的Swift扩展,可为任何子UIView类创建边框:import UIKitextension 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)  }}迅捷3extension 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)    }}

梦里花落0921

在以下类别中实施: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;@endUIButton + 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
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

iOS