Extensions and Helpers for Code Reduction
AppStore JSON APIs
Today's lesson we'll go ahead and implement the rest of our cell by including the three screenshot UIImageViews. In order to accomplish this, we'll be adding a few more UIStackViews that lay out our views horizontally and vertically. At the end of the lesson, I'll go over how to make use of extensions and helper subclasses to greatly reduce the code that you have in your views. Doing this is optional but I highly recommend that you make things as compact as possible for easier maintenance further down the road.

Comments (9)
ezechinwa
4 years ago
Hi, I kinda got lost, Is there any reason why using a negative trailingAnchor of -16 and using a positive value of 16 in your library achieved the same result, sir? And in the commented code, you didn't give a margin top and bottom of 16 but did in the library. Cheers
tsangaris
4 years ago
If you check the library it already uses the negative value (anchoredConstraints.trailing = trailingAnchor.constraint(equalTo: trailing, constant: -padding.right)). That is why Brian is using a positive value when he uses the extension.
ezechinwa
4 years ago
Thanks tsangaris
ezechinwa
4 years ago
Wow, thanks for the helper classes, Will be using them one every projects ;)
serxhio
4 years ago
you might as well check: https://cocoapods.org/pods/LBTAComponents , Brian's library! Not only includes the UIView + Layout extensions but also 2 more very important components to make your life a little easier :))
pawelcichonski
4 years ago
A week ago I had my first interview in UK for Junior iOS Dev. Didn't get it. First, they didn't like the fact I don't know how to handle storyboards, all of their project use that. We also had a conversation on auto layout, I said that I use NSLayoutConstraint (unless I have to modify constraints for animation and have a separate property, like in your video for sliding menu) otherwise just to speed things up: 'LBTA Components'. They've never heard of it and commented shortly that bringing to the project sb's addons not only risk with outdated code to constantly updated swift, other team members might not understand it. A bit nervous, but I think I need to have more of these interviews to know how to handle the conversation.
Brian Voong
4 years ago
pawelcichonski
4 years ago
Cheers. Not like I'm complaining, just realising the more I get into this I discover how much things I don't know yet :) They said that they appreciate a no storyboard coder as they had loads of people who just learn how to create a buttons on famous 'udemy' and not much beyond. And these questions are so random like you prepare yourself on Json or cache images, lifecycle, core data, hell no... I got something on sockets, pubsub, then 'what could you use instead of RestKit?'. Brain zero. Life :) Preparing, not letting go though :)
Brian Voong
4 years ago
applebyte
4 years ago
https://ui8.net is also a pretty cool place to get designs.
Brian Voong
4 years ago
Shqtucke
4 years ago
I have a dataSet that I created while coded in storyboard that I would like to populate this app with... I have an error on the code below. -> computed property must have an explicit type... I have no idea what I'm missing. Code: override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { var cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellId, for: indexPath) as? RestaurantResultCell { cell?.configureCell(category: data.casinos[indexPath.row]) return cell } return UICollectionViewCell() //return cell }
Shqtucke
4 years ago
Never mind... I forgot my if let.... walking away from it really does work.
Brian Voong
4 years ago
romulusc
4 years ago
started this course with LBTATools on board, awesome, great work professor. SearchResultsCell stackView only 4 lines of code: let stackView = stack(hstack(appIconImageView, stack(nameLabel, categoryLabel, ratingsLabel), getButton, spacing: 12, alignment: .center).padLeft(16).padRight(16).padTop(16), hstack(screenshotImageView1, screenshotImageView2, screenshotImageView3, spacing: 12, distribution: .fillEqually).padLeft(16).padRight(16).padTop(8))
njdavis
4 years ago
Do you have any idea why my view wouldn't update when rotating the device? My cell won't fill the width of the screen when the phone is rotated. However, when I open the app for the first time in landscape, the view is fitted to the correct width.
Tokyojogo
3 years ago
Hi Brian, What is the reason why this was made into a class (VerticalStackView) as opposed to making it an extension like the one you did for the constraints? Thanks.
grim_reaper
3 years ago
Hi Brian, I am totally new to swift and although I am kinda getting what is being done and since the code is readable, it helps a lot. I was wondering if you recommend any swift courses or practice materials that will help me get a hang of things, I was also looking at swift playground, is that any good?
ezechinwa
3 years ago
Hi Brian, I created a horizontal stack view helper import UIKit class HorizontalStackView: UIStackView { init(arrangedSubviews: [UIView], spacing: CGFloat = 0 , alignment: UIStackView.Alignment = .center) { super.init(frame: .zero) arrangedSubviews.forEach({addArrangedSubview($0)}) self.spacing = spacing self.axis = .horizontal self.alignment = alignment } required init(coder: NSCoder) { fatalError("init(coder:) has not been implemented") } }
HELP & SUPPORT