lunes, 31 de octubre de 2016

iOS: Objective C code in Swift

1) Create a header file: ProjectName-Bridging-Header.h
2) Physically it place it inside your project’s folder (not in the root)
3) In Xcode it should appear inside your project’s folder
4) In “Build Settings”:
a) Place the path to your header in “Objective-C Bridging Header”. For example: “ProjectFolder/ProjectName-Bridging-Header.h”
b) Make sure “Install Objective-C Compatibility Header” is set to “Yes”
c) Under “Linking”, “Other Linker Flags” should include “-ObjC”

6) Inside your bridging header import all your necessary headers.

martes, 18 de octubre de 2016

Android: setSelection spinner without triggering onItemSelected

spinner.setSelection(position, false);

Also you need to place "spinner.setOnItemSelectedListener(...)" AFTER "spinner.setAdapter(...)"

martes, 11 de octubre de 2016

iOS: Swift code in objective C project

1) Create new *.swift file (in Xcode) or add it by using Finder
2) Add swift bridging empty header if Xcode have not done this before (see 4 below)
3) Implement your Swift class by using @objc attribute:
import UIKit

@objc class Hello: NSObject {
    func sayHello() {
        print("Hi there!")
    }
}
4) Open Build Settings and check those parameters:
    Product Module Name : myproject
    Defines Module : YES
    Embedded Content Contains Swift : YES
    Install Objective-C Compatibility Header : YES
    Objective-C Bridging Header : $(SRCROOT)/Sources/SwiftBridging.h
5) Import header (which is auto generated by Xcode) in your *.m file
    #import "myproject-Swift.h"
6) Clean and rebuild your Xcode project
7) Profit!

miércoles, 5 de octubre de 2016

iOS: UITextView text content doesn't start from the top

Swift

override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()
        
        reachOutTextView.setContentOffset(CGPointMake(0, -14), animated: false)

    }

martes, 4 de octubre de 2016

iOS: Round image view

Swift
     
        imageView.layer.cornerRadiusimageView.frame.height/2
        imageView.clipsToBounds = true

Objective C

self.imageView.layer.cornerRadius = self. imageView.frame.size.width/2;
self. imageView.layer.borderColor = [UIColor whiteColor].CGColor;
self. imageView.layer.borderWidth = 2;

self. imageView.layer.masksToBounds = YES;

Interface Builder



Note: 

1) If you are using constraints make sure that the constraint height is the same as the image view size in the interface builder
2) You can't set border color in IB


iOS: Present view controller modally over current context

Swift

let storyboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
            let vc: UIViewController = storyboard.instantiateViewController(withIdentifier: "AddPaymentViewController")
            vc.modalPresentationStyle = UIModalPresentationStyle.overFullScreen;
            vc.modalTransitionStyle = UIModalTransitionStyle.coverVertical;
           
           
            self.present(vc, animated: true, completion: nil)

lunes, 3 de octubre de 2016

iOS: Screen scale / Scaling

Swift

let px = 1 / UIScreen.mainScreen().scale

Objective C


let px = 1 / [UIScreen mainScreen].scale