Skip to content

Instantly share code, notes, and snippets.

View jeffbailey's full-sized avatar

Jeff Bailey jeffbailey

View GitHub Profile
[NSLayoutConstraint constraintWithItem:view
attribute:NSLayoutAttributeCenterX
relatedBy:NSLayoutRelationEqual
toItem:view.superview
attribute:NSLayoutAttributeCenterX
multiplier:1.f constant:0.f];
@jeffbailey
jeffbailey / UIActionSheet Button Title Color
Last active December 26, 2015 01:08
UIActionSheet: Use tintColor for button titles.
// Iterate through the sub views of the action sheet
for (id actionSheetSubview in sheet.subviews) {
// Change the font color if the sub view is a UIButton
if ([actionSheetSubview isKindOfClass:[UIButton class]]) {
UIButton *button = (UIButton *)actionSheetSubview;
[button setTitleColor:self.view.tintColor forState:UIControlStateNormal];
[button setTitleColor:self.view.tintColor forState:UIControlStateSelected];
[button setTitleColor:self.view.tintColor forState:UIControlStateHighlighted];
}
@jeffbailey
jeffbailey / WeakSelf
Last active January 1, 2016 09:09
Weak Self using typeof
__weak typeof(self) weakSelf = self
foo.myBlock = ^{
typeof(self) strongSelf = weakSelf;
};
@jeffbailey
jeffbailey / iOS7StatusBarStyle
Last active January 1, 2016 11:49
iOS 7 override the status bar style for a UIViewController. You can also call the setNeedsStatusBarAppearanceUpdate method on the view controller to force the methods to be called again.
@implementation MyViewController
- (UIStatusBarStyle)preferredStatusBarStyle {
return UIStatusBarStyleDefault;
}
- (BOOL) prefersStatusBarHidden {
return NO;
}
@jeffbailey
jeffbailey / DynamicTypeWithCustomFonts
Last active January 1, 2016 11:59
How to use custom fonts that respond to Dynamic Type size changes.
NSNoticationCenter *center = [NSNotificationCenter defaultCenter];
[center addObserver: self selector:@selector(dynamicTypeChanged:) name: UIContentSizeCategoryDidChangeNotification
object:nil];
- (void) dynamicTypeChanged:(NSNotification *) note {
UIApplication *app = [UIApplication sharedApplication];
NSString *category = app.preferredContentSizeCategory;
if ([category isEqual:UIContentSizeCategorySmall]) {
@jeffbailey
jeffbailey / iOSVersionCheck
Created December 27, 2013 10:43
Conditional code based on iOS version
float versionNumber = floor(NSFoundationVersionNumber);
if (versionNumber <= NSFoundationVersionNumber_iOS_6_1) {
// use iOS 6 style appearance
} else {
// use iOS 7 style appearance
}
@jeffbailey
jeffbailey / ShowGrabberForSwipe
Created January 6, 2014 19:43
Show grabber instead of swiping open notification or control center. Useful if you're app supports swiping from the top or bottom of the screen.
<key>UIStatusBarHidden</key><true/>
<key>UIViewControllerBasedStatusBarAppearance</key><false/>
@jeffbailey
jeffbailey / AssociatedObjectProperty
Created January 21, 2014 01:00
Technique for using associated objects to add a property to a class using a category. See http://iosdevelopertips.com/objective-c/adding-properties-category-using-associated-objects.html.
@interface UIImage (Tagged)
@property (nonatomic, copy) NSString *tag;
@end
Then we implement the setter and getter for the property using the associated object runtime methods:
#import <objc/runtime.h>
static const void *ImageTagKey = &ImageTagKey;
@jeffbailey
jeffbailey / CropUIImage
Created January 22, 2014 18:52
Crop a UIImage
CGImageRef image = CGImageCreateWithImageInRect([orginalImage CGImage],cropRect);
UIImage *cropedImage = [UIImage imageWithCGImage:image];
CGImageRelease(image);
@jeffbailey
jeffbailey / CircleUIImageViewMask
Created January 25, 2014 12:25
Mask an UIImageView to a circle.
- (void)viewDidLayoutSubviews
{
[super viewDidLayoutSubviews];
self.imageView.layer.masksToBounds = YES;
self.imageView.layer.cornerRadius = self.imageView.bounds.size.width / 2.;
}