Skip to content

Commit

Permalink
addSubviews optimization extension
Browse files Browse the repository at this point in the history
  • Loading branch information
grenos committed Apr 13, 2020
1 parent a51ea50 commit c3e239e
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 13 deletions.
4 changes: 4 additions & 0 deletions GHFollowers.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
7DB7350F243F8AC100E7B09B /* FavoriteCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7DB7350E243F8AC100E7B09B /* FavoriteCell.swift */; };
7DB7351224409BFE00E7B09B /* GFTabBarController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7DB7351124409BFE00E7B09B /* GFTabBarController.swift */; };
7DB735142443929200E7B09B /* GFDataLoadingVC.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7DB735132443929200E7B09B /* GFDataLoadingVC.swift */; };
7DB7351624447F5800E7B09B /* UIView+EXt.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7DB7351524447F5800E7B09B /* UIView+EXt.swift */; };
7DFAC88A242A86D200F5780C /* GFAlertContainerView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7DFAC889242A86D200F5780C /* GFAlertContainerView.swift */; };
/* End PBXBuildFile section */

Expand Down Expand Up @@ -83,6 +84,7 @@
7DB7350E243F8AC100E7B09B /* FavoriteCell.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FavoriteCell.swift; sourceTree = "<group>"; };
7DB7351124409BFE00E7B09B /* GFTabBarController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = GFTabBarController.swift; sourceTree = "<group>"; };
7DB735132443929200E7B09B /* GFDataLoadingVC.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = GFDataLoadingVC.swift; sourceTree = "<group>"; };
7DB7351524447F5800E7B09B /* UIView+EXt.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "UIView+EXt.swift"; sourceTree = "<group>"; };
7DFAC889242A86D200F5780C /* GFAlertContainerView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = GFAlertContainerView.swift; sourceTree = "<group>"; };
/* End PBXFileReference section */

Expand Down Expand Up @@ -170,6 +172,7 @@
7D98BB4B242A7327006C53E9 /* UIViewController+EXT.swift */,
7DB735082438AD0F00E7B09B /* Date+Ext.swift */,
7DB7350A2438AD9000E7B09B /* String+Ext.swift */,
7DB7351524447F5800E7B09B /* UIView+EXt.swift */,
);
path = Extensions;
sourceTree = "<group>";
Expand Down Expand Up @@ -357,6 +360,7 @@
files = (
7DFAC88A242A86D200F5780C /* GFAlertContainerView.swift in Sources */,
7D98BB4224291DF3006C53E9 /* GFTextField.swift in Sources */,
7DB7351624447F5800E7B09B /* UIView+EXt.swift in Sources */,
7D98BB3E242907BB006C53E9 /* FavoriteListVC.swift in Sources */,
7D98BB40242917E4006C53E9 /* GFButton.swift in Sources */,
7D98BB3C24290771006C53E9 /* SearchVC.swift in Sources */,
Expand Down
Binary file not shown.
12 changes: 8 additions & 4 deletions GHFollowers/Components/ViewControllers/GFAlertVC.swift
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ class GFAlertVC: UIViewController {
super.viewDidLoad()
view.backgroundColor = UIColor(red: 0, green: 0, blue: 0, alpha: 0.75)

// we can add subviews like this in the begining
view.addSubviews(containerView, titleLabel, CTAButton, messageLabel)

configureContainerView()
configureTitleLabel()
configureButton()
Expand All @@ -50,7 +53,7 @@ class GFAlertVC: UIViewController {
// MARK: UI Functions

func configureContainerView() {
view.addSubview(containerView)
//view.addSubview(containerView)

NSLayoutConstraint.activate([
// center vertically to screen
Expand All @@ -66,7 +69,8 @@ class GFAlertVC: UIViewController {


func configureTitleLabel() {
containerView.addSubview(titleLabel)
//containerView.addSubview(titleLabel)

// Nil Coalescing ?? -- if nil use what comes after the ?? --
titleLabel.text = alertTitle ?? "Something went wrong"

Expand All @@ -80,7 +84,7 @@ class GFAlertVC: UIViewController {


func configureButton() {
containerView.addSubview(CTAButton)
//containerView.addSubview(CTAButton)
CTAButton.setTitle(buttonTitle ?? "Ok", for: .normal)
CTAButton.addTarget(self, action: #selector(dismissVC), for: .touchUpInside)

Expand All @@ -94,7 +98,7 @@ class GFAlertVC: UIViewController {


func configureMessageLabel() {
containerView.addSubview(messageLabel)
//containerView.addSubview(messageLabel)
messageLabel.text = message ?? "Unable to complete request"
messageLabel.numberOfLines = 4

Expand Down
15 changes: 9 additions & 6 deletions GHFollowers/Components/ViewControllers/GFUserInfoheaderVC.swift
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,15 @@ class GFUserInfoheaderVC: UIViewController {

// add all the subView here
func addSubViews() {
view.addSubview(avatarImageView)
view.addSubview(usernameLabel)
view.addSubview(nameLabel)
view.addSubview(locationImageView)
view.addSubview(locationLabel)
view.addSubview(bioLabel)

view.addSubviews(
avatarImageView,
usernameLabel,
nameLabel,
locationImageView,
locationLabel,
bioLabel
)
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,9 @@ class GFItemInfoVC: UIViewController {


private func layoutUI() {
view.addSubview(stackView)
view.addSubview(actionButton)
// custom extension
view.addSubviews(stackView, actionButton)

stackView.translatesAutoresizingMaskIntoConstraints = false
let padding: CGFloat = 20

Expand Down
24 changes: 24 additions & 0 deletions GHFollowers/Extensions/UIView+EXt.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
//
// UIView+EXt.swift
// GHFollowers
//
// Created by Vasileios Gkreen on 13/04/2020.
// Copyright © 2020 Vasileios Gkreen. All rights reserved.
//

import UIKit

extension UIView {


// VARIADIC PARAMETERS
// add 3 dots after the parameter and we can add as many params as we want that are all repreented in an array
// exactly as a rest operator in JS
func addSubviews(_ views: UIView...) {
for view in views {
addSubview(view)
}
}


}

0 comments on commit c3e239e

Please sign in to comment.