Skip to content

Instantly share code, notes, and snippets.

@thornpig
thornpig / gist:e7f6d3b2d40fd2b11038
Last active January 20, 2016 23:27
NSUInteger vs NSInteger
NSUInteger a = 0;
NSInteger b = 3;
NSInteger c = 1;
BOOL d = c >= a - b; //d is NO because a - b is implicitly converted to NSUInteger 4294967294
NSString *string = @"";
BOOL e = c >= string.length - b; // e is NO because string.lenth returns NSUInteger.
@thornpig
thornpig / gist:7b5784fa9d19a1eb217b
Created January 19, 2016 01:10
Fix to font attribute not being applied to label attreibutedtext
self.newsFeedDescriptionLabel.font = nil; //without this line the font attribute may not apply to the label
@thornpig
thornpig / gist:dca4383c8494bae3a5ee
Created January 20, 2016 20:27
Work around for the problem: Frame of container view is not right when its frame is setup via autolayout
-(void)viewDidLayoutSubviews
{
self.containerView.frame = self.view.bounds;
self.currentVC.view.frame = self.containerView.bounds;
}
-(void)showChildViewController:(UIViewController *)childVC
{
[self addChildViewController:childVC];
self.containerView.frame = self.view.bounds;
@thornpig
thornpig / gist:01e63fad50e848cb2f4b
Created January 20, 2016 23:24
To set the back button title for a view controller without changing it's title
To set the back button title for a view controller without changing it's title you use:
Objective-C:
self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"" style:self.navigationItem.backBarButtonItem.style target:nil action:nil];
Swift:
navigationItem.backBarButtonItem = UIBarButtonItem(title: "", style: .Plain, target: nil, action: nil)
To be clear, this is done on the view controller that you would see if you hit the back button.
i.e. instead of seeing '< Settings' you want to just see '<' then on your SettingsViewController you would put this in your init.
@thornpig
thornpig / gist:9d7152a6d91059c1c4b5
Created January 21, 2016 02:09
When using auto layout, retrieving subview frames should be done after frames are determined based on constraints
In UIViewController subclasses
-(void)viewDidLayoutSubviews
{
self.containerView.frame = self.view.bounds;
self.currentVC.view.frame = self.containerView.bounds;
}
In UIView subclasses
-(void)layoutSubviews
{
@thornpig
thornpig / gist:a865172fa58792025a4e
Created January 21, 2016 21:13
Must call [super layoutSubviews] in -layoutSubviews
-(void)layoutSubviews
{
[super layoutSubviews];
....
}
@thornpig
thornpig / gist:827ef418ab523fa8511d
Last active January 22, 2016 22:08
Sequence of constraint activation/deactivation matters
-(void)updateConstraints
{
[super updateConstraints];
if (self.binaryFiles.count > 0)
{
//deactivate first, then activate, then change constant
[NSLayoutConstraint deactivateConstraints: @[self.attachmentButtonConstraintHeight]];
[NSLayoutConstraint activateConstraints: @[self.attachmentButtonConstraintAspectRatio]];
// self.attachmentButtonConstraintHeight.active = NO;
// self.attachmentButtonConstraintAspectRatio.active = YES;
@thornpig
thornpig / gist:4bf981c37c912d3fe570
Created January 23, 2016 01:02
Create image thumbnail with imageIO on background can make scroll smoother
CFDataRef imageData = (__bridge CFDataRef)[self getData];
CGImageSourceRef imageSourceRef = CGImageSourceCreateWithData(imageData, nil);
CFDictionaryRef options = (__bridge CFDictionaryRef) @{
(id) kCGImageSourceCreateThumbnailWithTransform : @YES,
(id) kCGImageSourceCreateThumbnailFromImageAlways : @YES,
(id) kCGImageSourceThumbnailMaxPixelSize : @(100)};
CGImageRef imageRef = CGImageSourceCreateThumbnailAtIndex(imageSourceRef, 0 , options);
thumbnail = [UIImage imageWithCGImage: imageRef];
return thumbnail;
@thornpig
thornpig / gist:8daeba3a42935718d8e7
Last active January 26, 2016 04:34
Make sure reused cell presents the right picture with GCD
id imageSource = cell.imageSource;
dispatch_async(queue, ^{
//block captures imageSource
UIImage *thumbNail = getImageFromSource(imageSource);
dispatch_sync(dispatch_get_main_queue(), ^{
//block captures cell
//if imageSource does not match cell.imageSource, it means the cell is resued before the background thread returns the image,
// and the image requested for the already invisible cell may be returned after the current cell obtains its image, in which case
// the returned image should not be used.
if ([imagaeSource isEqualToArray: cell.imageSource])
@thornpig
thornpig / gist:215990f2f3f4349f41f1
Created February 2, 2016 20:57
Force early layout with layoutIfNeeded if the some frame information is needed before the drawing cycle begins.
-(void)viewWillAppear:(BOOL)animated
{
//force the layout before the drawing cycle begins, so that when the tableview setup its cells the subviews of the cell
already have the frame information of the cell. For example, with this line, the text view inside the cell can use
the cell width to calculate depth to accormodate its text.
[self.view layoutIfNeeded];
[self.tableView reloadData];
}