UiViewController takes a long time to display - ios

then I have a UIViewController class that is called instantiated and displayed by a method, the problem lies in the fact that the end of self.present the view is not completely displayed, but only a part is displayed, after about 5-6 seconds, the rest of the view is also displayed! How can I make the view appear completely at the end of self.present?
Swift Code:
///First Method (It's call nextviewcontroller
var ret: Bool = false
let c = c(c: self.c!)
c.crea(n: self.txtn.text!, completion: { result in
ret = result
})
if(ret == true)
{
let viewnext = NextViewController(c: c)
self.present(viewnext, animated: true, completion: { () in print("Done") })
}
//NextViewController.swift
class NextViewController: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource {
//Oggetti Privati
private var TextViewDesc: UITextView = UITextView()
private var pickerviewStato: UIPickerView = UIPickerView()
private var labelnameINFO: UILabel = UILabel()
private var labelClientINFO: UILabel = UILabel()
private var labelfilINFO: UILabel = UILabel()
private var labelClient: UILabel = UILabel()
private var labelfil: UILabel = UILabel()
private var buttonris: UIButton = UIButton(type: .custom)
private var buttonKilometri = UIButton(type: .custom)
private var buttonfield = UIButton(type: .custom)
private var buttonprint = UIButton(type: .custom)
private var buttonReturn = UIButton()
private let statoCantiere = ["InCorso", "Chiuso", "Lavoro terminato inserire bolle"]
private var immagineStatoCantiere: UIImageView = UIImageView()
private let DistLtoR: CGFloat = 130
private let DistRtoL: CGFloat = 50
//Oggetti Pubblici
var c: c
init(c: c) {
c = c
super.init(nibName: nil, bundle: nil)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func viewDidLoad() {
super.viewDidLoad()
hideKeyboardWhenTappedAround()
self.view.backgroundColor = UIColor.white
initView()
}
func initView() {
labelnameINFO = UILabel(frame: CGRect(x: 20, y: 60, width: 120, height: 21))
labelnameINFO.text = "Cantiere:"
labelnameINFO.font = UIFont(name: "HelveticaNeue-Bold", size: 16.0)
self.view.addSubview(labelnameINFO)
let labelname = UILabel(frame: CGRect(x: 100, y: 60, width: 200, height: 21))
labelname.text = CantiereInterno.Getname()
self.view.addSubview(labelname)
labelClientINFO = UILabel(frame: CGRect(x: 20, y: 100, width: 130, height: 21))
labelClientINFO.text = "Ragione Sociale:"
labelClientINFO.font = UIFont(name: "HelveticaNeue-Bold", size: 16.0)
self.view.addSubview(labelClientINFO)
labelClient = UILabel(frame: CGRect(x: 180, y: 100, width: 180, height: 21))
labelClient.text = c.GetRagioneSociale()
self.view.addSubview(labelClient)
//Configurazioe label fil
labelfilINFO = UILabel(frame: CGRect(x: 20, y: 140, width: 130, height: 21))
labelfilINFO.text = "fil:"
labelfilINFO.font = UIFont(name: "HelveticaNeue-Bold", size: 16.0)
self.view.addSubview(labelfilINFO)
labelfil = UILabel(frame: CGRect(x: 100, y: 140, width: 180, height: 21))
labelfil.text = c.Getfil()
self.view.addSubview(labelfil)
let labelStato = UILabel(frame: CGRect(x: 20, y: 190, width: 130, height: 21))
labelStato.text = "Stato:"
labelStato.font = UIFont(name: "HelveticaNeue-Bold", size: 16.0)
self.view.addSubview(labelStato)
self.pickerviewStato = UIPickerView(frame: CGRect(x: 90, y: 180, width: 160, height: 50))
self.pickerviewStato.delegate = self as UIPickerViewDelegate
self.pickerviewStato.dataSource = self as UIPickerViewDataSource
self.pickerviewStato.backgroundColor = UIColor.gray
self.view.addSubview(pickerviewStato)
immagineStatoCantiere = UIImageView(frame: CGRect(x: (self.view.frame.width / 2) + 50, y: 180, width: 50, height: 50));
immagineStatoCantiere.image = UIImage(named: "InCorso.png")
immagineStatoCantiere.backgroundColor = UIColor.gray
self.view.addSubview(immagineStatoCantiere)
self.TextViewDesc = UITextView(frame: CGRect(x: 0, y: 250, width: self.view.frame.width, height: 80))
self.automaticallyAdjustsScrollViewInsets = false
TextViewDesc.font = UIFont(name: "HelveticaNeue-Bold", size: 18.0)
TextViewDesc.textAlignment = NSTextAlignment.justified
TextViewDesc.textColor = UIColor.blue
TextViewDesc.isEditable = true
TextViewDesc.backgroundColor = UIColor.lightGray
TextViewDesc.text = c.GetDescrizioneEstesa()
self.view.addSubview(TextViewDesc)
buttonris.frame = CGRect(x: (self.view.frame.width / 2) - DistLtoR, y: 380, width: 100, height: 100)
buttonris.layer.cornerRadius = 0.5 * buttonris.bounds.size.width
buttonris.clipsToBounds = true
buttonris.setImage(UIImage(named: "risorse_umane.png"), for: .normal)
buttonris.backgroundColor = UIColor.lightGray
buttonris.addTarget(self, action: #selector(risbuttonAction), for: .touchUpInside)
self.view.addSubview(buttonris)
buttonKilometri.frame = CGRect(x: (self.view.frame.width / 2) + DistRtoL, y: 380, width: 100, height: 100)
buttonKilometri.layer.cornerRadius = 0.5 * buttonKilometri.bounds.size.width
buttonKilometri.clipsToBounds = true
buttonKilometri.setImage(UIImage(named: "kilometri.png"), for: .normal)
buttonKilometri.backgroundColor = UIColor.lightGray
buttonKilometri.addTarget(self, action: #selector(KilometributtonAction), for: .touchUpInside)
self.view.addSubview(buttonKilometri)
buttonfield.frame = CGRect(x: (self.view.frame.width / 2) - DistLtoR, y: 500, width: 100, height: 100)
buttonfield.layer.cornerRadius = 0.5 * buttonfield.bounds.size.width
buttonfield.clipsToBounds = true
buttonfield.setImage(UIImage(named: "field.png"), for: .normal)
buttonfield.backgroundColor = UIColor.lightGray
buttonfield.addTarget(self, action: #selector(fieldbuttonAction), for: .touchUpInside)
self.view.addSubview(buttonfield)
buttonprint.frame = CGRect(x: (self.view.frame.width / 2) + DistRtoL, y: 500, width: 100, height: 100)
buttonprint.layer.cornerRadius = 0.5 * buttonfield.bounds.size.width
buttonprint.clipsToBounds = true
buttonprint.setImage(UIImage(named: "print.png"), for: .normal)
buttonprint.backgroundColor = UIColor.lightGray
buttonprint.addTarget(self, action: #selector(RapportinobuttonAction), for: .touchUpInside)
self.view.addSubview(buttonprint)
buttonprint.isHidden = false
buttonReturn.frame = CGRect(x: (self.view.frame.width / 2), y: 600, width: 80, height: 80)
buttonReturn.backgroundColor = UIColor.white
buttonReturn.setImage(UIImage(named: "return.png"), for: .normal)
buttonReturn.setTitle("Go Home", for: .normal)
buttonReturn.addTarget(self, action: #selector(ReturnToHomebuttonAction), for: .touchUpInside)
self.view.addSubview(buttonReturn)
}

Call initView in viewwillappear:
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
if labelnameINFO == nil{
self.initView()
}
}

Related

NavigationItem.titleView is not getting the right size

I'm trying to set a custom view on a large navigationBar, but it insists on staying at 44px.
func setLargeNavBarForSection(_ section: String, subTitle: String, studyLogoName: String) -> Void {
if let navigationBar = self.navigationController?.navigationBar {
navigationBar.barTintColor = .cyan
navigationBar.shadowImage = UIImage()
let customView = UIView(frame: CGRect(x: 0, y: 0, width: navigationBar.frame.width, height: 96.0))
let sectionNameFrame = CGRect(x: 80, y : 23, width : 150, height : 41)
let subTitleFrame = CGRect(x: 80, y: 53, width: 200, height: 14)
let studyImageView = UIImageView(frame: CGRect(x: 20, y: 25, width: 45, height: 45))
studyImageView.image = UIImage(named: studyLogoName)?.imageWithInsets(insets: UIEdgeInsets(top: 10, left: 10, bottom: 10, right: 10))
studyImageView.contentMode = .scaleAspectFit
studyImageView.backgroundColor = .white
studyImageView.layer.cornerRadius = 22
studyImageView.addShadow(radius: 3, opacity: 0.1, x: 1, y: 2)
let myStudiesButton = UIButton(frame: CGRect(x: navigationBar.frame.width-96, y: 36, width: 76, height: 26))
myStudiesButton.setImage(UIImage(named: "switchStudies"), for: .normal)
myStudiesButton.addTarget(self, action: Selector(("switchStudies")), for: .touchUpInside)
myStudiesButton.showsTouchWhenHighlighted = true
let sectionNameLabel = UILabel(frame: sectionNameFrame)
sectionNameLabel.text = section
sectionNameLabel.textAlignment = .left
sectionNameLabel.font = UIFont(name: "Montserrat-Bold", size: 18)
sectionNameLabel.textColor = .black
let subTitleLabel = UILabel(frame: subTitleFrame)
subTitleLabel.text = subTitle
subTitleLabel.textAlignment = .left
subTitleLabel.font = UIFont(name: "Montserrat-Medium", size: 11)
subTitleLabel.textColor = .blueGrey
// customView.addSubview(studyImageView)
// customView.addSubview(myStudiesButton)
// customView.addSubview(sectionNameLabel)
// customView.addSubview(subTitleLabel)
navigationItem.largeTitleDisplayMode = .always
customView.backgroundColor = .red
navigationItem.titleView = customView
}
}
navigationController?.navigationBar.prefersLargeTitles = true get called in the viewDidLoad on the ViewController.

Custom NavigationBar in iOS Swift

I'm creating custom navigationbar in swift for every screen. I'm calling this function at viewWillAppear while first time loading it is coming properly if First Time Loading Screen. I move to another screen it is changing. Next View screen navigation title view is shifted. if I came back to previous screen then also it was shiftedenter image description here. I'm unable to find the bug where code was wrong.left and right navigation items are placing fine . But navigationtitle view only getting shifting.
extension UIViewController {
func reightMenuItems(_ isMenuBtn: Bool, _ navigationItem:UINavigationItem) {
if isMenuBtn {
let leftView = UIView()
leftView.backgroundColor = .clear
leftView.frame = CGRect(x: 0, y: 0, width: 40, height: 44)
let menuImage = UIImageView(image:#imageLiteral(resourceName: "MenuIcon"))
menuImage.contentMode = .scaleAspectFit
menuImage.frame = CGRect(x: 2.5, y: 6, width: 35, height: 32)
leftView.addSubview(menuImage)
let followButton = UIButton(type: .system)
followButton.frame = CGRect(x: 0, y: 0, width: 40, height: 44)
followButton.contentEdgeInsets = UIEdgeInsetsMake(0, -20, 0, 0)
followButton.addTarget(self, action: #selector(self.btnClickMenu), for: .touchUpInside)
leftView.addSubview(followButton)
navigationItem.leftBarButtonItems = [UIBarButtonItem(customView: leftView)]
}else{
let leftView = UIView()
leftView.backgroundColor = .clear
leftView.frame = CGRect(x: 0, y: 0, width: 40, height: 44)
let menuImage = UIImageView(image:#imageLiteral(resourceName: "arrow_white-left"))
menuImage.contentMode = .scaleToFill
menuImage.frame = CGRect(x: 12.5, y: 10.5, width: 15, height: 25)
leftView.addSubview(menuImage)
let followButton = UIButton(type: .system)
followButton.frame = CGRect(x: 0, y: 0, width: 40, height: 44)
leftView.addSubview(followButton)
followButton.addTarget(self, action: #selector(self.btnClickBack), for: .touchUpInside)
navigationItem.leftBarButtonItems = [UIBarButtonItem(customView: leftView)]
}
}
fileprivate func leftMenuItems(_ isCart: Bool, _ navigationItem: UINavigationItem) {
//lecftButtons
if isCart {
let reightView = UIView()
reightView.backgroundColor = .clear
reightView.frame = CGRect(x: 0, y: 0, width: 40, height: 44)
let menuImage = UIImageView(image:#imageLiteral(resourceName: "Cart"))
menuImage.contentMode = .scaleToFill
menuImage.frame = CGRect(x: 2.5, y: 6, width: 35, height: 34)
reightView.addSubview(menuImage)
let cartBtn = UIButton(type: .system)
cartBtn.frame = CGRect(x: 0, y: 0, width: 40, height: 44)
reightView.addSubview(cartBtn)
cartBtn.addTarget(self, action: #selector(self.btnClickCart), for: .touchUpInside)
navigationItem.rightBarButtonItems = [UIBarButtonItem(customView: reightView)]
}else{
let composeButton = BtnFontBold20()
composeButton.setTitle("Done", for: .normal)
composeButton.titleLabel?.textColor = .white
composeButton.frame = CGRect(x: 0, y: 00, width: 34, height: 44)
composeButton.contentEdgeInsets = UIEdgeInsetsMake(0, 20, 0, 0)
composeButton.addTarget(self, action: #selector(self.btnClickCart), for: .touchUpInside)
navigationItem.rightBarButtonItems = [UIBarButtonItem(customView: composeButton)]
}
}
func setupNavItems(_ navigationItem: UINavigationItem , day:String = "",address:String = "",type:Int,isMenuBtn:Bool,isCart:Bool,title:String = "") {
//NavigationItem
self.navigationController?.isNavigationBarHidden = false
self.navigationController?.navigationBar.isTranslucent = false
self.navigationController?.navigationBar.setValue(true, forKey: "hidesShadow")
//Shadow
self.navigationController?.navigationBar.layer.shadowOffset = CGSize(width: 1.5, height: 1.5)
self.navigationController?.navigationBar.layer.shadowRadius = 2.0
self.navigationController?.navigationBar.layer.shadowOpacity = 0.5
//reight Menu Items
reightMenuItems(isMenuBtn, navigationItem)
leftMenuItems(isCart, navigationItem)
let height = navigationController?.navigationBar.frame.height ?? 0
let leftButtonWidth: CGFloat = 90 // left padding
let width2 = AppConstants.screenWidth - leftButtonWidth
print(width2)
let Titleview = UIView(frame: CGRect(x: 0, y: 0, width: width2, height: height))
Titleview.backgroundColor = .clear
navigationItem.titleView = Titleview
if type == 1 {
let dayLbl = FontBold18(frame: CGRect(x: 0, y: 0, width: min(width2/2,40), height: height))
dayLbl.text = day
dayLbl.textColor = .white
dayLbl.textAlignment = .left
dayLbl.numberOfLines = 1
Titleview.addSubview(dayLbl)
let DirectImag = UIImageView()
DirectImag.contentMode = .scaleAspectFit
DirectImag.image = #imageLiteral(resourceName: "RightDirection")
Titleview.addSubview(DirectImag)
DirectImag.translatesAutoresizingMaskIntoConstraints = false
DirectImag.widthAnchor.constraint(equalToConstant: 15).isActive = true
DirectImag.heightAnchor.constraint(equalToConstant: 15).isActive = true
DirectImag.leadingAnchor.constraint(equalTo: dayLbl.trailingAnchor, constant:5).isActive = true
DirectImag.centerYAnchor.constraint(equalTo: dayLbl.centerYAnchor).isActive = true
let dayBtn = UIButton(type: .system)
dayBtn.addTarget(self, action: #selector(self.btnClickASAPType), for: .touchUpInside)
Titleview.addSubview(dayBtn)
dayBtn.translatesAutoresizingMaskIntoConstraints = false
dayBtn.centerYAnchor.constraint(equalTo: dayLbl.centerYAnchor).isActive = true
dayBtn.centerXAnchor.constraint(equalTo: dayLbl.centerXAnchor, constant: 0).isActive = true
dayBtn.heightAnchor.constraint(equalTo: dayLbl.heightAnchor).isActive = true
dayBtn.widthAnchor.constraint(equalTo: dayLbl.widthAnchor , constant:25).isActive = true
let addLbl = FontBold18()
addLbl.textColor = .white
addLbl.textAlignment = .left
addLbl.numberOfLines = 1
Titleview.addSubview(addLbl)
addLbl.text = address
addLbl.translatesAutoresizingMaskIntoConstraints = false
addLbl.leadingAnchor.constraint(equalTo: DirectImag.trailingAnchor,constant:5).isActive = true
addLbl.heightAnchor.constraint(equalToConstant: height).isActive = true
addLbl.centerYAnchor.constraint(equalTo: Titleview.centerYAnchor).isActive = true
addLbl.widthAnchor.constraint(lessThanOrEqualToConstant: (width2 - dayLbl.frame.width) * 0.9).isActive = true
let downImag = UIImageView()
downImag.contentMode = .scaleAspectFit
downImag.image = #imageLiteral(resourceName: "down-arrow-white")
Titleview.addSubview(downImag)
downImag.translatesAutoresizingMaskIntoConstraints = false
downImag.widthAnchor.constraint(equalToConstant: 15).isActive = true
downImag.heightAnchor.constraint(equalToConstant: 15).isActive = true
downImag.leadingAnchor.constraint(equalTo: addLbl.trailingAnchor, constant:5).isActive = true
downImag.centerYAnchor.constraint(equalTo: addLbl.centerYAnchor).isActive = true
let addressBtn = UIButton(type: .system)
addressBtn.backgroundColor = .clear
addressBtn.addTarget(self, action: #selector(self.btnClickAddress), for: .touchUpInside)
Titleview.addSubview(addressBtn)
addressBtn.translatesAutoresizingMaskIntoConstraints = false
addressBtn.centerYAnchor.constraint(equalTo: addLbl.centerYAnchor).isActive = true
addressBtn.leadingAnchor.constraint(equalTo: addLbl.leadingAnchor, constant: 0).isActive = true
addressBtn.heightAnchor.constraint(equalTo: addLbl.heightAnchor).isActive = true
addressBtn.widthAnchor.constraint(equalTo: addLbl.widthAnchor).isActive = true
}
else if type == 2{
let dayLbl = FontBold18()
dayLbl.text = title
dayLbl.textColor = .white
dayLbl.textAlignment = .center
dayLbl.numberOfLines = 1
Titleview.addSubview(dayLbl)
dayLbl.translatesAutoresizingMaskIntoConstraints = false
dayLbl.centerXAnchor.constraint(equalTo: Titleview.centerXAnchor).isActive = true
dayLbl.centerYAnchor.constraint(equalTo: Titleview.centerYAnchor).isActive = true
}
}
#objc func btnClickASAPType() {
print("DayExt")
// self.overriderFunc()
}
#objc func btnClickAddress() {
print("AddressExt")
}
#objc func btnClickCart() {
print("CartExt")
}
#objc func btnClickMenu() {
print("MenuExt")
}
#objc func btnClickBack() {
print("backExt")
self.navigationController?.popViewController(animated: true)
}
}
calling setupNavigation Method in viewDidLoad
self.setupNavItems( self.navigationItem, day:"Today", address:"Jayanagar 4th Block", type: 1,isMenuBtn: false,isCart: true)

Setup rightBarButtonItem in navigationBar

I have stuck on this bug for days. I have a problem with rightBarButtonItem. I want to create a custom view. Inside custom view, there are 2 items: label, imageView. When I type this:
let customView = UIView()
customView.translatesAutoresizingMaskIntoConstraints = false
customView.backgroundColor = .black
customView.frame = CGRect(x: 0, y: 0, width: 90, height: 40)
let coinLabelNavigationBar = UILabel()
coinLabelNavigationBar.frame = CGRect(x: 40, y: 0, width: 30, height: 25)
coinLabelNavigationBar.translatesAutoresizingMaskIntoConstraints = false
coinLabelNavigationBar.text = "200"
coinLabelNavigationBar.font = UIFont(name: ".SFUIText-Medium", size: 20)
coinLabelNavigationBar.textColor = UIColor.white
let coinImage = UIImageView()
coinImage.frame = CGRect(x: 75, y: 0, width: 25, height: 25)
coinImage.translatesAutoresizingMaskIntoConstraints = false
coinImage.image = UIImage(named: "Coin")
customView.addSubview(coinLabelNavigationBar)
customView.addSubview(coinImage)
let rightBarButton = UIBarButtonItem(customView: customView)
navigationItem.rightBarButtonItem = rightBarButton
It doesn't work, navigationBar shows me this:
Do you have any idea, how to fix it?
Thanks for your answers.
try this -
let customView = UIView()
// customView.translatesAutoresizingMaskIntoConstraints = false
customView.backgroundColor = .black
customView.frame = CGRect(x: 0, y: 0, width: 90, height: 40)
let coinLabelNavigationBar = UILabel()
coinLabelNavigationBar.frame = CGRect(x: 40, y: 0, width: 30, height: 25)
// coinLabelNavigationBar.translatesAutoresizingMaskIntoConstraints = false
coinLabelNavigationBar.text = "200"
coinLabelNavigationBar.font = UIFont(name: ".SFUIText-Medium", size: 20)
coinLabelNavigationBar.textColor = UIColor.white
let coinImage = UIImageView()
coinImage.frame = CGRect(x: 75, y: 0, width: 25, height: 25)
// coinImage.translatesAutoresizingMaskIntoConstraints = false
coinImage.image = UIImage(named: "coin.jpg")
customView.addSubview(coinLabelNavigationBar)
customView.addSubview(coinImage)
let rightBarButton = UIBarButtonItem(customView: customView)
navigationItem.rightBarButtonItem = rightBarButton
For Swift 3.0:
let btn1 = UIButton(type: .custom)
btn1.setImage(UIImage(named: "imagename"), for: .normal)
btn1.frame = CGRect(x: 0, y: 0, width: 30, height: 30)
btn1.addTarget(self, action: #selector(Class.Methodname), for: .touchUpInside)
let item1 = UIBarButtonItem(customView: btn1)
let btn2 = UIButton(type: .custom)
btn2.setImage(UIImage(named: "imagename"), for: .normal)
btn2.frame = CGRect(x: 0, y: 0, width: 30, height: 30)
btn2.addTarget(self, action: #selector(Class.MethodName), for: .touchUpInside)
let item2 = UIBarButtonItem(customView: btn2)
self.navigationItem.setRightBarButtonItems([item1,item2], animated: true)

How to add multiple button in a scrollView programmatically

I am trying to put multiple buttons in a scrollView but it just scrolls background view.
class HomeVC: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
navigationItem.title = "Home"
let scrollView = UIScrollView()
let view = UIView()
scrollView.frame = self.view.bounds
self.view.backgroundColor = .green
scrollView.backgroundColor = .blue
scrollView.addSubview(view)
self.view.addSubview(scrollView)
scrollView.isPagingEnabled = true
scrollView.contentSize = CGSize(width: self.view.frame.size.width, height: self.view.frame.size.height * 3)
view.frame = CGRect(x: 0, y: self.view.frame.size.height, width: self.view.frame.size.width, height: self.view.frame.size.height)
view.backgroundColor = .yellow
attractivePlaceButtonSetup()
eatDrinkButtonSetup()
ShoppingButtonSetup()
festivalEventButtonSetup()
hotelGuestHouseButtonSetup()
travellerEssentialButtonSetup()
dealButtonSetup()
seeDoButtonSetup()
}
I wrote this code for button frame
func eatDrinkButtonSetup(){
let button = UIButton()
button.frame = CGRect(x: 5, y: 225, width: self.view.frame.size.width - 10, height: 150)
button.setTitle("Eat & Drink", for: .normal)
button.setBackgroundImage(#imageLiteral(resourceName: "imageName"), for: .normal)
button.titleEdgeInsets = UIEdgeInsets(top: -120, left: -200, bottom: 0, right: 0)
button.addTarget(self, action: #selector(targetEatDrink), for: .touchUpInside)
view.addSubview(button)
}
}
I also try to such way but it just scroll a button.
scrollView.addSubview(attractivePlaceButtonSetup)
self.view.addSubview(scrollView)
//Try this...
//Background Scroll Creation
var stickersScrollViewCount = 0
func stickersScrollContents() {
var xCoord: CGFloat = 5
let yCoord: CGFloat = 5
let buttonWidth:CGFloat = 45.0
let buttonHeight: CGFloat = 45.0
let gapBetweenButtons: CGFloat = 5
for i in 0..<stickersImageArray.count{
stickersScrollViewCount = i
// Button properties
let filterButton = UIButton(type: .custom)
filterButton.frame = CGRect(x: xCoord, y: yCoord, width: buttonWidth, height: buttonHeight)
filterButton.tag = stickersScrollViewCount
filterButton.backgroundColor = UIColor.clear
filterButton.setTitleColor(UIColor.white, for: .normal)
filterButton.titleLabel?.adjustsFontSizeToFitWidth = true
filterButton.showsTouchWhenHighlighted = true
let myimage = UIImage(named: stickersImageArray[stickersScrollViewCount])
filterButton.setImage(myimage, for: .normal)
filterButton.addTarget(self, action:#selector(StickersActionTapped), for: .touchUpInside)
filterButton.layer.cornerRadius = 5
filterButton.clipsToBounds = true
xCoord += buttonWidth + gapBetweenButtons
bgScrollView.addSubview(filterButton)
}
bgScrollView.contentSize = CGSize(width: buttonWidth * CGFloat(stickersScrollViewCount+2), height: yCoord)
}
//Call the function where ever you want viewDidLoad() or on Button Click!!
//Hope this helps!!!

App crashes due to use a custom view with images on different view

My app is working fine with all iOS simulator above iOS v7.0 and also working on most of device but its crash into iPod touch iOS 8.4. I am not able to identify what was the issue with my app, because when I connect this device with Xcode 6.3 and test my app then its working fine.
So my app crash into iPod touch iOS V8.4 in two different cases:
Download app from Test fairy into device and open screen on which I
am using my custom view
Connect device with Xcode and run app into device(With connected
device app work fine even on screen where I used my custom view).
Then disconnect device, Run app again but this time my app was crash
when I reach on screen with custom view.
Custom view code :
import UIKit
class OutfitView:UIView {
var screenSize = SCREEN_SIZE()
let bigScaleImageView: UIImageView!
let rigthSideImageOne: UIImageView!
let rigthSideImageTwo: UIImageView!
let rigthSideImageThree: UIImageView!
let oufitNameLbl: UILabel!
let oufitItemCountLbl: UILabel!
let likeBtn: UIButton!
let likeCountBtn: UIButton!
let rigthSideFooterLbl:UILabel!
let moreImageLbl: UILabel!
override init(frame: CGRect ) {
var yposition = CGFloat(0)
oufitNameLbl = UILabel(frame: CGRect(x: 0, y: yposition, width: (screenSize.width - 20) * (70/100), height: 30))
oufitNameLbl.text = "Outfit Name"
oufitNameLbl.textAlignment = .Left
oufitNameLbl.textColor = UIColor.blackColor()
oufitNameLbl.font = UIFont.systemFontOfSize(18)
oufitItemCountLbl = UILabel(frame: CGRect(x: oufitNameLbl.frame.width, y: yposition, width: (screenSize.width - 20) * (30/100), height: 30))
oufitItemCountLbl.text = "8 Items"
oufitItemCountLbl.textAlignment = .Right
oufitItemCountLbl.textColor = UIColor.grayColor()
oufitItemCountLbl.font = UIFont.systemFontOfSize(14)
yposition += oufitNameLbl.frame.height
var height = frame.height - 80
bigScaleImageView = UIImageView(frame: CGRect(x: 0, y: yposition, width: (screenSize.width - 20) * (70/100) - 4, height: height))
bigScaleImageView.contentMode = UIViewContentMode.ScaleAspectFit
bigScaleImageView.layer.borderWidth = 2
bigScaleImageView.layer.borderColor = UIColor.whiteColor().CGColor
bigScaleImageView.backgroundColor = UIColor.whiteColor()
rigthSideImageOne = UIImageView(frame: CGRect(x: bigScaleImageView.frame.size.width+4, y: yposition, width: (screenSize.width - 20) * (30/100), height: (height - 8)/3))
rigthSideImageOne.layer.borderWidth = 2
rigthSideImageOne.layer.borderColor = UIColor.whiteColor().CGColor
rigthSideImageOne.contentMode = UIViewContentMode.ScaleAspectFit
rigthSideImageOne.backgroundColor = UIColor.whiteColor()
rigthSideImageTwo = UIImageView(frame: CGRect(x: bigScaleImageView.frame.size.width+4, y: yposition + rigthSideImageOne.frame.height+4, width: (screenSize.width - 20) * (30/100), height: (height - 8)/3))
rigthSideImageTwo.layer.borderWidth = 2
rigthSideImageTwo.layer.borderColor = UIColor.whiteColor().CGColor
rigthSideImageTwo.contentMode = UIViewContentMode.ScaleAspectFit
rigthSideImageTwo.backgroundColor = UIColor.whiteColor()
rigthSideImageThree = UIImageView(frame: CGRect(x: bigScaleImageView.frame.size.width+4, y:yposition + (rigthSideImageOne.frame.height + rigthSideImageTwo.frame.height)+8, width: (screenSize.width - 20) * (30/100), height: (height - 8)/3))
rigthSideImageThree.layer.borderWidth = 2
rigthSideImageThree.layer.borderColor = UIColor.whiteColor().CGColor
rigthSideImageThree.contentMode = UIViewContentMode.ScaleAspectFit
rigthSideImageThree.backgroundColor = UIColor.whiteColor()
moreImageLbl = UILabel(frame: CGRect(x: bigScaleImageView.frame.size.width+4, y: yposition + (rigthSideImageOne.frame.height + rigthSideImageTwo.frame.height)+8, width: (screenSize.width - 20) * (30/100), height: (height - 8)/3))
moreImageLbl.text = "+4"
moreImageLbl.textAlignment = .Center
moreImageLbl.textColor = UIColor.whiteColor()
moreImageLbl.backgroundColor = Utilities.getGSBlackColor(alphaValue: CGFloat(0.6))
yposition += bigScaleImageView.frame.height
let editLikeView = UIView(frame: CGRect(x: 0, y: yposition, width: screenSize.width - 20, height: 50))
likeBtn = UIButton(frame: CGRect(x: 0, y: 8, width: 15, height: 15))
likeBtn.setTitle("", forState: .Normal)
likeBtn.setTitle("", forState: .Selected)
//likeBtn.backgroundColor = UIColor.whiteColor()
var image = UIImage(named: "gray_heart")?.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)
likeBtn.setImage(image, forState: .Normal)
likeBtn.setTitleColor(UIColor.whiteColor(), forState: .Normal)
likeBtn.setTitleColor(Utilities.getGSRedColor(), forState: .Selected)
likeBtn.titleLabel?.font = UIFont.systemFontOfSize(14)
likeBtn.titleLabel?.textAlignment = .Left
likeBtn.userInteractionEnabled = true
editLikeView.addSubview(likeBtn)
likeCountBtn = UIButton(frame: CGRect(x: 13, y: 0, width: 30, height: 30))
likeCountBtn.setTitle("28", forState: .Normal)
likeCountBtn.setTitle("28", forState: .Selected)
likeCountBtn.setTitleColor(Utilities.getMasterColor(), forState: .Normal)
likeCountBtn.setTitleColor(Utilities.getMasterColor(), forState: .Selected)
likeCountBtn.titleLabel?.font = UIFont.systemFontOfSize(14)
likeCountBtn.titleLabel?.textAlignment = .Left
likeCountBtn.userInteractionEnabled = true
editLikeView.addSubview(likeCountBtn)
rigthSideFooterLbl = UILabel(frame: CGRect(x: (screenSize.width-20) - ((screenSize.width - 20) * (0.80)), y: 0, width:(screenSize.width - 20) * (0.80), height: 30) )
rigthSideFooterLbl.text = "CreatedBy:#XXXXXXXXX"
rigthSideFooterLbl.textAlignment = .Right
rigthSideFooterLbl.textColor = UIColor.blackColor()
rigthSideFooterLbl.font = UIFont.systemFontOfSize(16)
editLikeView.addSubview(rigthSideFooterLbl)
super.init(frame: frame)
self.addSubview(oufitNameLbl)
self.addSubview(oufitItemCountLbl)
self.addSubview(editLikeView)
self.addSubview(bigScaleImageView)
self.addSubview(rigthSideImageOne)
self.addSubview(rigthSideImageTwo)
self.addSubview(rigthSideImageThree)
self.addSubview(moreImageLbl)
}
required init(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
And code where I used my custom view:
for i in 1...3{
var outfitView = OutfitView(frame: CGRect(x: 10, y: yposition, width: screenSize.width - 20, height: 330))
outfitView.bigScaleImageView.image = image1
outfitView.rigthSideImageOne.image = image2
outfitView.rigthSideImageTwo.image = image3
outfitView.rigthSideImageThree.image = image4
itemDetailView.frame.size.height += (outfitView.frame.height + 10 )
yposition += outfitView.frame.height + 10
itemDetailView.addSubview(outfitView)
}

Resources