Skip to content

Instantly share code, notes, and snippets.

@Inferis
Created October 9, 2012 19:21
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Inferis/3860850 to your computer and use it in GitHub Desktop.
Save Inferis/3860850 to your computer and use it in GitHub Desktop.
Apply a rounded overlay to a viewcontroller.
//
// UIView+RoundedOverlay.h
//
// Created by Tom Adriaenssen on 05/04/12.
//
#import <UIKit/UIKit.h>
@interface UIViewController (RoundedOverlay)
- (void)applyRoundedOverlay;
@end
//
// UIView+RoundedOverlay.m
//
// Created by Tom Adriaenssen on 05/04/12.
//
#import "UIViewController+RoundedOverlay.h"
#import <QuartzCore/QuartzCore.h>
#define TAG 86456134
@implementation UIViewController (RoundedOverlay)
- (void)applyRoundedOverlay {
UIView* selfView = self.navigationController ? self.navigationController.view : self.view;
UIImageView* view = (UIImageView*)[selfView viewWithTag:TAG];
if (!view) {
UIImage* image = [UIImage imageNamed:@"rounded-overlay.png"];
image = [[self image:image cropped:(CGRect) { 0, 0, image.size.width, 5 }] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 5, 0, 5)];
view = [[UIImageView alloc] initWithImage:image];
view.layer.shouldRasterize = YES;
view.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleBottomMargin;
view.tag = TAG;
view.userInteractionEnabled = NO;
}
else
[view removeFromSuperview];
view.frame = (CGRect) { 0, 0, selfView.bounds.size.width, 5 };
if (selfView.superview == nil || ([selfView.superview.superview isKindOfClass:[UIWindow class]] && selfView.superview.frame.origin.y == 0)) {
view.frame = CGRectOffsetTopAndShrink(view.frame, [[UIApplication sharedApplication] statusBarFrame].size.height);
}
[selfView addSubview:view];
view = (UIImageView*)[selfView viewWithTag:TAG+1];
if (!view) {
UIImage* image = [UIImage imageNamed:@"rounded-overlay.png"];
image = [[self image:image cropped:(CGRect) { 0, image.size.height-5, image.size.width, 5 }] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 5, 0, 5)];
view = [[UIImageView alloc] initWithImage:image];
view.layer.shouldRasterize = YES;
view.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleBottomMargin;
view.tag = TAG+1;
view.userInteractionEnabled = NO;
}
else
[view removeFromSuperview];
view.frame = (CGRect) { 0, selfView.bounds.size.height-5, selfView.bounds.size.width, 5 };
[selfView addSubview:view];
}
- (UIImage*)image:(UIImage*)image cropped:(CGRect)cropRect {
if (image.scale != 1.0f) {
cropRect = CGRectMake(cropRect.origin.x * image.scale,
cropRect.origin.y * image.scale,
cropRect.size.width * image.scale,
cropRect.size.height * image.scale);
}
CGImageRef imageRef = CGImageCreateWithImageInRect([image CGImage], cropRect);
image = [UIImage imageWithCGImage:imageRef scale:image.scale orientation:image.imageOrientation];
CGImageRelease(imageRef);
return image;
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment