Friday, September 8, 2017

Realm/RLMArray.h file not found and Could not build Objective-C module 'Realm'

Run Clean Build Folder...(Hold down option while clicking Product in the Xcode menu shows the Clean Build Folder...option) it fixes the issue for me

Courtesy : https://github.com/realm/realm-cocoa/issues/3551

Monday, September 4, 2017

iOS UIScrollViews have extra space on top


From iOS 7 onwards, a ViewController's view starts at absolute position (0,0), which means it's underneath the Nav Bar. 

So if we have a tableView or any other type of Scroll view in the View, the there is a chance that the top content might get hidden by the top bars. So ViewControllers automatically adjust the content inset of subviews which are of type UIScrollView to consider the space taken by the bars. 

If the ViewController is inside a NavigationController, the space typically is 64 (20 for the status bar + 44 for the Navigation Bar). Otherwise it's just 20, for the Status Bar

We can disable this automatic adjustment by setting: 

self.automaticallyAdjustsScrollViewInsets = NO;



This just got deprectated in iOS 11. The new method to fix this is : 

scrollView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever

Wednesday, July 12, 2017

UICollectionView Sticky Headers & Footers

From iOS 9, UICollectionViewFlowLayout has two very handy properties to pin the header & footer to the top/bottom of the collection view bounds

1. sectionHeadersPinToVisibleBounds

When this property is true, section header views scroll with content until they reach the top of the screen, at which point they are pinned to the upper bounds of the collection view. Each new header view that scrolls to the top of the screen pushes the previously pinned header view offscreen. 

2. sectionFootersPinToVisibleBounds

When this property is true, section footer views scroll with content until they reach the bottom of the screen, at which point they are pinned to the lower bounds of the collection view. Each new footer view that scrolls to the bottom of the screen pushes the previously pinned footer view offscreen.

Friday, March 17, 2017

Applying Gradient Background Color to view

let gradientLayer:CAGradientLayer = CAGradientLayer()
gradientLayer.frame = (self.bounds)
gradientLayer.colors = [UIColor.white.cgColorUIColor.purple.cgColor]

self.layer.insertSublayer(gradientLayer, at: 0)

Wednesday, February 22, 2017

To find if current device is iPhone 4 or 4S


iPhone 4 and 4S have shorter screens, so we need to adjust the UI height in many cases. This macro makes it easier : 

#define IS_IPHONE5 (([[UIScreen mainScreen] bounds].size.height - 568)? NO:YES)

UIButton Title gets cut at the top when using custom font


Like shown in the image, if we change the font in a UIButton, the title text gets cut. The fix is simple, just use: 

backBtn?.contentVerticalAlignment = .fill



Wednesday, February 15, 2017

Shadow on UIView using UIBezierPath


//Shadow on the right side
UIBezierPath *shadowPath = [UIBezierPath bezierPathWithRect:CGRectMake(view.width - 1, -4, 2, view.height)];
view.layer.masksToBounds = NO;
view.layer.shadowColor   = [UIColor blackColor].CGColor;
view.layer.shadowOffset  = CGSizeMake(0.0f, 5.0f);
view.layer.shadowOpacity = 0.4f;
view.layer.shadowPath    = shadowPath.CGPath;

view.layer.shadowRadius  = 2.0f;

//Shadow on the bottom side
let shadowPath:UIBezierPath   = UIBezierPath.init(rect:CGRect(x: -4y: self.height - 1width : self.width,height: 1))
self.layer.masksToBounds      = false
self.layer.shadowColor        = UIColor.black.cgColor
self.layer.shadowOffset       = CGSize(width : 0.0, height :1.0)
self.layer.shadowOpacity      = 0.4
self.layer.shadowPath         = shadowPath.cgPath
self.layer.shadowRadius       = 2.0

[There are lot of customisation options for shadows using UIBezierPath... 

Check links below: 

https://nachbaur.com/2010/11/16/fun-shadow-effects-using-custom-calayer-shadowpaths/

https://markpospesel.wordpress.com/2012/04/03/on-the-importance-of-setting-shadowpath/



Swift 3.0 version : 


        //Shadow on the bottom side
        let shadowPath:UIBezierPath   = UIBezierPath.init(rect:CGRect(x: -4,
                                                                      y: self.height - 1,
                                                                 width : self.width,
                                                                 height: 2))
        self.layer.masksToBounds      = false
        self.layer.shadowColor        = UIColor.black.cgColor
        self.layer.shadowOffset       = CGSize(width : 0.0, height :5.0)
        self.layer.shadowOpacity      = 0.4
        self.layer.shadowPath         = shadowPath.cgPath
        

        self.layer.shadowRadius       = 2.0

Show only the top and right borders of a view

CALayer *bordersLayer               = [CALayer layer];
bordersLayer.borderColor            = COLOR_GRAY_212.CGColor;
bordersLayer.borderWidth            = 1;
bordersLayer.frame                  = CGRectMake(-1, 0, CGRectGetWidth(view.frame)+1, CGRectGetHeight(view.frame)+1);

[view.layer addSublayer:bordersLayer];