12 Weeks as a iOS Developer Intern — Week Six
Time has been flying by, thanks for coming along with me on this journey!
Here’s a few things I learned during week 6:
UICollectionViews
Any number of cells can be registered in a single collectionView. I needed to have two custom cells with different heights in different sections in a single collectionView. For testing purposes, I used the data’s section name to tell the collectionView which custom cell it should be using to draw the cell. Optimally, it is not the best to use a string condition because it may change later on.
- Make sure to register all the custom cells you need when adding the collectionView
As you can see, different custom cells in the single collectionView are set according to section name:
The following methods can be implemented on the flow layout if the delegate object has not implemented these methods:
Circular UIViews
I needed to create a circular UIView with a background color and a white border. However the standard built in method that Apple provides to set the border causes the background color to bleed a bit around the white border by a few pixels. It can’t be seen on a simulator but can be seen on an iOS device. It is especially apparent if the view with white border has a white background. The pixel bleed can’t be seen if it is on top of something that has a color.
To achieve pixel perfection:
- I created a “fake” border by drawing a white circle UIView and drawing a smaller UIView with a background color on top of it, creating an illusion of a white border.
RxSwift
I always thought that ReplaySubjects, PublishSubjects, BehaviorSubjects needed to be given an observable stream of events through binding but it turns out you can pass it an object with an onNext event as long as the object is of the same type as the ReplaySubject, PublishSubject, BehaviorSubject.
private let option = ReplaySubject<ProductOption>.create()func update(option: ProductOption) {self.option.onNext(option)}
I’m passing a ProductOption in the update function to the ReplaySubject option.
Ternary Operators ? :
contentView.backgroundColor = selection ? .modifierSelected : .modifierNotSelected
If selection is true set background color to .modifierSelected else set background color to .modifierNotSelected
The ternary operator is quite magical to me because it saves so much code and is easier to read. If the ternary operator wasn’t used, it could be written as :
if isSelected {contentView.backgroundColor = .modifierSelected} else {contentView.backgroundColor = .modifierNotSelected
The latter is much clunkier to look at~
Thank you for reading!
Read about week five as an iOS developer intern here
Show your support with 👏 claps below, follow me on my iOS developer journey 😊 on Twitter