Skip to content

Instantly share code, notes, and snippets.

@xingheng
Last active July 12, 2023 16:43
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xingheng/a0338cc677e84eb5585681a9ab4fcb8e to your computer and use it in GitHub Desktop.
Save xingheng/a0338cc677e84eb5585681a9ab4fcb8e to your computer and use it in GitHub Desktop.
Add edge borders to UIView with autolayout, inspired from https://stackoverflow.com/a/23157272/1677041
//
// 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
//
// 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