Thursday, November 17, 2016

Hide status bar

In Swift 3, to hide the status bar, simply add the following to your view controller.
override var prefersStatusBarHidden: Bool {  
    return true  
}  

However, sometimes you might just want to do it in a completion block, or within some animation block. In that case, add
UIApplication.shared.isStatusBarHidden = true
And toggle off when desired

Friday, November 4, 2016

Check optional + empty string together

Apart from checking nil from an optional string, it's pretty common for us to check whether or not that optional as an empty string. e.g: ""

I found this wonderful solution, credits to StackOverflow once more.

(string ?? "").isEmpty

Source: http://stackoverflow.com/questions/29381994/swift-check-string-for-nil-empty

Thursday, October 13, 2016

How to figure out the font to assign it programatically in swift



Assigning fonts to texts programatically can be a pain due to the parameters passed being a hardcoded string, hence no auto completion, what's worse is the font's name isn't always the same as the filename. Furthermore fonts are grouped by font families, which may contain multiple fonts.




Firstly, run print(UIFont.familyNames) to print out every single family name in an array.



["Copperplate", "Heiti SC", "Times New Roman"]




You can then

print(UIFont.fontNames(forFamilyName: "Times New Roman"))




Which outputs to



["TimesNewRomanPSMT", "TimesNewRomanPS-BoldItalicMT", "TimesNewRomanPS-ItalicMT", "TimesNewRomanPS-BoldMT"]




Then you can finally let font = UIFont(name: "TimesNewRomanPS-BoldMT", size: 16)

Wednesday, September 21, 2016

Remove unwanted message from Xcode 8 console logs

1- From xCode menu open: Product > Scheme > Edit Scheme
2- On your Environment Variables set OS_ACTIVITY_MODE = disable

Note that the value is disable NOT false

Tuesday, September 20, 2016

Corner Radius in Swift 2.3 and above

Since Swift 2.3 onwards, it is now required to define the corner radius of your views after your view has loaded, hence, move the corner radius value assignment to viewDidLayoutSubviews() else your view will just not be visible from your app, even though it's viewable in the story board or the view hierarchy.

As for views in a table view cell, I've seen people getting their views rounded by assigning cornerRadius inside layoutSubviews(), but I didn't have any success with that, the following worked for me even though I personally don't like it.


Add self.layoutIfNeeded() right after super.awakeFromNib() to force layout

Thursday, July 14, 2016

ITMS-90205, ITMS-90206

If anyone ever encounters this, do the following two things
1) Add a run script for your extension target.
cd "${CONFIGURATION_BUILD_DIR}/${UNLOCALIZED_RESOURCES_FOLDER_PATH}/"
if [[ -d "Frameworks" ]]; then
rm -fr Frameworks

fi
2) http://stackoverflow.com/questions/25777958/validation-error-invalid-bundle-the-bundle-at-contains-disallowed-file-fr/25789145#25789145
Set Embedded Content Contains Swift Code: NO for the extension target only.

Credits to my superior who solved the former.

Thursday, June 23, 2016

Whitespace for UITextView

There are different margins/paddings between UILabel and UITextView
Example:
If you wanted to remove the whitespace for UITextView so you can have what you expect from autolayout, do this.

self.commentTextView.textContainerInset = UIEdgeInsetsZero
self.commentTextView.textContainer.lineFragmentPadding = 0


Thursday, June 2, 2016

tab bar title inherits navbar title

I've always conveniently used self.title = "New Title" to update my navigation bar's title

However, I recently noticed that will affect the tabbar title as well, to prevent the tabbar from inheriting the navbar's title, use

self.navigationItem.title  = "My New Title"

to prevent changing the tab bar item's title as well

Thursday, January 28, 2016

Search bar disappears when presenting search controller


If you have your navigation bar's translucent being false, do turn it on before presenting search controller. else there will be a weird UI bug where your search bar just disappears.

Do remember to change translucent back to false when dismissing search controller.