Skip to content

Instantly share code, notes, and snippets.

@rsattar
Created December 15, 2014 22:58
Show Gist options
  • Save rsattar/30467a69ff79ae402e0b to your computer and use it in GitHub Desktop.
Save rsattar/30467a69ff79ae402e0b to your computer and use it in GitHub Desktop.
UIView+BorderViews - A simple way to add a throwaway border view to a particular edge of a UIView
//
// UIView+BorderViews.h
// Cluster
//
// Created by Rizwan Sattar on 4/1/14.
// Copyright (c) 2014 Cluster Labs, Inc. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface UIView (BorderViews)
- (UIView *) addBorderViewToEdge:(UIRectEdge)edge
borderSize:(CGFloat)borderSize
borderColor:(UIColor *)borderColor
inset:(CGFloat)inset;
@end
//
// UIView+BorderViews.m
// Cluster
//
// Created by Rizwan Sattar on 4/1/14.
// Copyright (c) 2014 Cluster Labs, Inc. All rights reserved.
//
#import "UIView+BorderViews.h"
@implementation UIView (BorderViews)
- (UIView *) addBorderViewToEdge:(UIRectEdge)edge
borderSize:(CGFloat)borderSize
borderColor:(UIColor *)borderColor
inset:(CGFloat)inset
{
if (edge == UIRectEdgeAll || edge == UIRectEdgeNone) {
return nil;
}
UIView *borderView = [[UIView alloc] initWithFrame:CGRectZero];
borderView.backgroundColor = borderColor;
CGRect bounds = self.bounds;
CGRect frame = bounds;
UIViewAutoresizing mask = UIViewAutoresizingNone;
switch (edge) {
case UIRectEdgeTop:
frame.size.height = borderSize;
frame.origin.y = inset;
mask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleBottomMargin;
break;
case UIRectEdgeBottom:
frame.size.height = borderSize;
frame.origin.y = CGRectGetHeight(bounds)-CGRectGetHeight(frame)-inset;
mask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin;
break;
case UIRectEdgeLeft:
frame.size.width = borderSize;
frame.origin.y = inset;
mask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleRightMargin;
break;
case UIRectEdgeRight:
frame.size.width = borderSize;
frame.origin.x = CGRectGetWidth(bounds)-CGRectGetWidth(frame)-inset;
mask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleLeftMargin;
default:
break;
}
borderView.frame = frame;
borderView.autoresizingMask = mask;
//[self insertSubview:borderView atIndex:0];
[self addSubview:borderView];
return borderView;
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment