Last active
July 12, 2023 16:43
Add edge borders to UIView with autolayout, inspired from https://stackoverflow.com/a/23157272/1677041
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// UIView+EdgeBorder.h | |
// DSUtility | |
// | |
// Created by WeiHan on 6/6/17. | |
// Copyright © 2017 WeiHan. All rights reserved. | |
// | |
#import <UIKit/UIKit.h> | |
@interface UIView (EdgeBorder) | |
- (void)setEdgeBorder:(UIRectEdge)option color:(UIColor *)color; | |
- (void)setEdgeBorder:(UIRectEdge)option color:(UIColor *)color thickness:(CGFloat)thickness; | |
- (void)setEdgeBorder:(UIRectEdge)option color:(UIColor *)color thickness:(CGFloat)thickness configuration:(void (^)(UIRectEdge option, UIView *border))block; | |
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// UIView+EdgeBorder.m | |
// DSUtility | |
// | |
// Created by WeiHan on 6/6/17. | |
// Copyright © 2017 WeiHan. All rights reserved. | |
// | |
// Inspired from https://stackoverflow.com/a/23157272/1677041 | |
// | |
#import "UIView+EdgeBorder.h" | |
@implementation UIView (EdgeBorder) | |
- (void)setEdgeBorder:(UIRectEdge)option color:(UIColor *)color | |
{ | |
[self setEdgeBorder:option color:color thickness:1.0]; | |
} | |
- (void)setEdgeBorder:(UIRectEdge)option color:(UIColor *)color thickness:(CGFloat)thickness | |
{ | |
[self setEdgeBorder:option color:color thickness:thickness configuration:nil]; | |
} | |
- (void)setEdgeBorder:(UIRectEdge)option color:(UIColor *)color thickness:(CGFloat)thickness configuration:(void (^)(UIRectEdge option, UIView *border))block | |
{ | |
#define EdgeConstraint(__attribute__, __view__) [NSLayoutConstraint constraintWithItem: __view__ attribute: __attribute__ relatedBy: NSLayoutRelationEqual toItem: self attribute: __attribute__ multiplier: 1.0 constant : 0] | |
#define WidthConstraint(__const_height__, __view__) [NSLayoutConstraint constraintWithItem: __view__ attribute: NSLayoutAttributeWidth relatedBy: NSLayoutRelationEqual toItem: self attribute: NSLayoutAttributeWidth multiplier : 0 constant: __const_height__] | |
#define HeightConstraint(__const_height__, __view__) [NSLayoutConstraint constraintWithItem:__view__ attribute: NSLayoutAttributeHeight relatedBy: NSLayoutRelationEqual toItem: self attribute: NSLayoutAttributeHeight multiplier : 0 constant: __const_height__] | |
UIView * (^ createBorder)(UIRectEdge) = ^UIView *(UIRectEdge op) { | |
UIView *border = [UIView new]; | |
border.translatesAutoresizingMaskIntoConstraints = NO; | |
border.backgroundColor = color; | |
!block ? : block(op, border); | |
[self addSubview:border]; | |
return border; | |
}; | |
if (option & UIRectEdgeTop) { | |
UIView *view = createBorder(UIRectEdgeLeft); | |
[self addConstraint:EdgeConstraint(NSLayoutAttributeTop, view)]; | |
[self addConstraint:EdgeConstraint(NSLayoutAttributeLeft, view)]; | |
[self addConstraint:EdgeConstraint(NSLayoutAttributeRight, view)]; | |
[self addConstraint:HeightConstraint(thickness, view)]; | |
} | |
if (option & UIRectEdgeLeft) { | |
UIView *view = createBorder(UIRectEdgeLeft); | |
[self addConstraint:EdgeConstraint(NSLayoutAttributeTop, view)]; | |
[self addConstraint:EdgeConstraint(NSLayoutAttributeLeft, view)]; | |
[self addConstraint:EdgeConstraint(NSLayoutAttributeBottom, view)]; | |
[self addConstraint:WidthConstraint(thickness, view)]; | |
} | |
if (option & UIRectEdgeBottom) { | |
UIView *view = createBorder(UIRectEdgeBottom); | |
[self addConstraint:EdgeConstraint(NSLayoutAttributeLeft, view)]; | |
[self addConstraint:EdgeConstraint(NSLayoutAttributeRight, view)]; | |
[self addConstraint:EdgeConstraint(NSLayoutAttributeBottom, view)]; | |
[self addConstraint:HeightConstraint(thickness, view)]; | |
} | |
if (option & UIRectEdgeRight) { | |
UIView *view = createBorder(UIRectEdgeRight); | |
[self addConstraint:EdgeConstraint(NSLayoutAttributeTop, view)]; | |
[self addConstraint:EdgeConstraint(NSLayoutAttributeRight, view)]; | |
[self addConstraint:EdgeConstraint(NSLayoutAttributeBottom, view)]; | |
[self addConstraint:WidthConstraint(thickness, view)]; | |
} | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment