How to automatically size UIScrollView to fit the content

The best method I’ve ever come across to update the content size of a UIScrollView based on its contained subviews: Objective-C CGRect contentRect = CGRectZero; for (UIView *view in self.scrollView.subviews) { contentRect = CGRectUnion(contentRect, view.frame); } self.scrollView.contentSize = contentRect.size; Swift let contentRect: CGRect = scrollView.subviews.reduce(into: .zero) { rect, view in rect = rect.union(view.frame) } scrollView.contentSize … Read more

Generate hash from UIImage

I wound up using the following code to accomplish the task. Note that this requires that you import <CommonCrypto/CommonDigest.h>: unsigned char result[CC_MD5_DIGEST_LENGTH]; NSData *imageData = [NSData dataWithData:UIImagePNGRepresentation(inImage)]; CC_MD5([imageData bytes], [imageData length], result); NSString *imageHash = [NSString stringWithFormat: @”%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X”, result[0], result[1], result[2], result[3], result[4], result[5], result[6], result[7], result[8], result[9], result[10], result[11], result[12], result[13], result[14], result[15] ];

applicationWillTerminate: not being called

From Apple docs: For applications that do not support background execution or are linked against iOS 3.x or earlier, this method is always called when the user quits the application. For applications that support background execution, this method is generally not called when the user quits the application because the application simply moves to the … Read more

UIControlEventTouchDragExit triggers when 100 pixels away from UIButton

Override continueTrackingWithTouch:withEvent: like this to send DragExit/DragOutside events inside of the default gutter: – (BOOL)continueTrackingWithTouch:(UITouch *)touch withEvent:(UIEvent *)event { CGFloat boundsExtension = 25.0f; CGRect outerBounds = CGRectInset(self.bounds, -1 * boundsExtension, -1 * boundsExtension); BOOL touchOutside = !CGRectContainsPoint(outerBounds, [touch locationInView:self]); if(touchOutside) { BOOL previousTouchInside = CGRectContainsPoint(outerBounds, [touch previousLocationInView:self]); if(previousTouchInside) { NSLog(@”Sending UIControlEventTouchDragExit”); [self sendActionsForControlEvents:UIControlEventTouchDragExit]; } else … Read more

Looping using NSRange

It kind of sounds like you’re expecting NSRange to be like a Python range object. It’s not; NSRange is simply a struct typedef struct _NSRange { NSUInteger location; NSUInteger length; } NSRange; not an object. Once you’ve created one, you can use its members in a plain old for loop: NSUInteger year; for(year = years.location; … Read more