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

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)
}

Related

UiViewController takes a long time to display

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()
}
}

Set background color for top safe area only in iOS 11

I'm new to Swift. I have set my top bar programmatically.It works fine for all versions except for iOS version 11.
I want to change the background color of the safe area in iPhone X. Presently I have just added the following code to hide the status bar.
override var prefersStatusBarHidden: Bool {
return true
}
I have created top bar using code:
//Top Bar
let topBar = UIView(frame:CGRect(x: 0,y: 0, width: width, height: 60))
topBar.backgroundColor = UIColor.white
topBar.layer.shadowColor = UIColor.gray.cgColor
topBar.layer.shadowOffset = CGSize(width: 0, height: 3)
topBar.layer.shadowOpacity = 1
topBar.layer.masksToBounds = false
topBar.layer.shadowRadius = 8.0;
//ImageView - Back Button
let backBtn = UIButton(frame:CGRect(x: 25, y: 18, width: 18, height: 34))
let backBtnImage = UIImage(named: "back_button") as UIImage?
backBtn.setImage(backBtnImage, for: .normal)
backBtn.layer.masksToBounds = true
backBtn.addTarget(self,action:#selector(backButtonClicked),
for:.touchUpInside)
//Label - Title
let titleLabel = UILabel(frame:CGRect(x: width * 0.3, y: 13, width: width * 0.55, height: 40))
titleLabel.text = "Favorites"
titleLabel.contentMode = UIViewContentMode.center
//include all in view
topBar.addSubview(titleLabel)
topBar.addSubview(backBtn)
containerView.addSubview(topBar)
Is there a way in which i can do it without using UINavigationBar or setting status bar.
Hi you can also try this one ,
need to add a view with same background which you set in your header view and add to container parent view .
please refer below code
//Top Bar
let safeAreaView = UIView(frame:CGrect(x: 0,y: 0, width: width, height: 40))
safeAreaView.backgroundColor = UIColor.white
self.view.addSubview(safeAreaView)
let topBar = UIView(frame:CGRect(x: 0,y: 0, width: width, height: 60))
topBar.backgroundColor = UIColor.white
topBar.layer.shadowColor = UIColor.gray.cgColor
topBar.layer.shadowOffset = CGSize(width: 0, height: 3)
topBar.layer.shadowOpacity = 1
topBar.layer.masksToBounds = false
topBar.layer.shadowRadius = 8.0;
//ImageView - Back Button
let backBtn = UIButton(frame:CGRect(x: 25, y: 18, width: 18, height: 34))
let backBtnImage = UIImage(named: "back_button") as UIImage?
backBtn.setImage(backBtnImage, for: .normal)
backBtn.layer.masksToBounds = true
backBtn.addTarget(self,action:#selector(backButtonClicked),
for:.touchUpInside)
//Label - Title
let titleLabel = UILabel(frame:CGRect(x: width * 0.3, y: 13, width: width * 0.55, height: 40))
titleLabel.text = "Favorites"
titleLabel.contentMode = UIViewContentMode.center
//include all in view
topBar.addSubview(titleLabel)
topBar.addSubview(backBtn)
containerView.addSubview(topBar)

How to create custom uiview that button can be clicked inside uiview?

I tried to create clicked scrollview button like this (at bottom):
this is my code so far:
scView = UIScrollView(frame: CGRect(x: 0, y: view.frame.maxY-110, width: view.bounds.width, height: 110))
self.view.addSubview(scView)
scView.backgroundColor = UIColor.lightGray
scView.translatesAutoresizingMaskIntoConstraints = false
for i in 0 ... 10 {
let myNewView=UIView(frame: CGRect(x: xOffset, y: CGFloat(buttonPadding), width: 200, height: 100))
myNewView.backgroundColor=UIColor.white
myNewView.layer.cornerRadius=10
self.scView.addSubview(myNewView)
let label = UILabel(frame: CGRect(x: xOffset, y: CGFloat(buttonPadding)+5, width: 150, height: 10))
label.center = CGPoint(x: 160, y: 285)
label.textAlignment = .left
label.text = "I'am a title label"
label.textColor = .black
myNewView.addSubview(label)
let ditancelabel = UILabel(frame: CGRect(x: xOffset, y: CGFloat(buttonPadding)+5, width: 50, height: 10))
ditancelabel.center = CGPoint(x: 160, y: 285)
ditancelabel.textAlignment = .right
ditancelabel.text = "0.04 km"
ditancelabel.textColor = .red
myNewView.addSubview(ditancelabel)
let textView = UITextView(frame: CGRect(x: xOffset, y: CGFloat(buttonPadding)+15, width: 200.0, height: 40.0))
self.automaticallyAdjustsScrollViewInsets = false
textView.center = self.view.center
textView.textAlignment = NSTextAlignment.justified
textView.textColor = UIColor.lightGray
textView.backgroundColor = UIColor.clear
myNewView.addSubview(textView)
let button = UIButton()
button.tag = i
button.backgroundColor = UIColor.white
button.setTitle("\(i)", for: .normal)
button.setTitleColor(.black, for: .normal)
button.backgroundColor = UIColor.clear
button.addTarget(self, action:#selector(handleRegister(sender:)), for: .touchUpInside)
button.frame = CGRect(x: xOffset, y: CGFloat(buttonPadding), width: 200, height: 100)
xOffset = xOffset + CGFloat(buttonPadding) + button.frame.size.width
myNewView.addSubview(button)
}
scView.contentSize = CGSize(width: xOffset, height: scView.frame.height)
but code result like this:
The first button can be clicked, but the other cannot be clicked. And 2 UILabels and 1 UITextView did not appear.
the handleRegister method is to get sender tag and print it as log to know the button can be clicked or not.
How to repair the code so it can be like the custom UIView in the above image?
I need to transfer label and textview text by button click to another viewcontroller which handle by seque in handleRegister.
1) You can't see labels and other control because of your container view size is (200, 100)
let myNewView=UIView(frame: CGRect(x: xOffset, y: CGFloat(buttonPadding), width: 200, height: 100))
and you set center of controls.
label.center = CGPoint(x: 160, y: 285)
ditancelabel.center = CGPoint(x: 160, y: 285)
This center point is not visible within container view. That's why you can't see labels within view. Just comment these line to set center of labels and you can see those perfectly.
2) I need to transfer label and textview text by button click to another viewcontroller which handle by seque in handleRegister.
I hope you have a array in which you stored data to display in your custom view, Just find that object from array based on button tag, and pass that object to another control.
3) The first button can be clicked, but the other cannot be clicked.
Again problem is same like for labels. You had set frame of button like this
button.frame = CGRect(x: xOffset, y: CGFloat(buttonPadding), width: 200, height: 100)
and increase xOffset by this line to create another view
xOffset = xOffset + CGFloat(buttonPadding) + button.frame.size.width
xOffset increased each time with the size of button. So if you print xOffset value it actually print like (assuming initial value of xOffset = 10, padding = 0)
for i in 0 ... 10 {
print(xOffset)
..... your other code
}
Output: will be like
10, 210, 410, 610...... Because of this your button is out of bound from container view and because of that you can't click on that.
I have updated your code, check below
scView = UIScrollView(frame: CGRect(x: 0, y: view.frame.maxY-110, width: view.bounds.width, height: 110))
self.view.addSubview(scView)
scView.backgroundColor = UIColor.lightGray
scView.translatesAutoresizingMaskIntoConstraints = false
for i in 0 ... 10 {
let myNewView=UIView(frame: CGRect(x: xOffset, y: CGFloat(buttonPadding), width: 200, height: 100))
myNewView.backgroundColor=UIColor.white
myNewView.layer.cornerRadius=10
self.scView.addSubview(myNewView)
let subVwOffset = 5
let label = UILabel(frame: CGRect(x: subVwOffset, y: CGFloat(buttonPadding)+5, width: 150, height: 10))
label.textAlignment = .left
label.text = "I'am a title label"
label.textColor = .black
myNewView.addSubview(label)
let ditancelabel = UILabel(frame: CGRect(x: subVwOffset, y: CGFloat(buttonPadding)+5, width: 50, height: 10))
ditancelabel.textAlignment = .right
ditancelabel.text = "0.04 km"
ditancelabel.textColor = .red
myNewView.addSubview(ditancelabel)
let textView = UITextView(frame: CGRect(x: subVwOffset, y: CGFloat(buttonPadding)+15, width: 200.0, height: 40.0))
self.automaticallyAdjustsScrollViewInsets = false
textView.center = self.view.center
textView.textAlignment = NSTextAlignment.justified
textView.textColor = UIColor.lightGray
textView.backgroundColor = UIColor.clear
myNewView.addSubview(textView)
let button = UIButton()
button.tag = i
button.backgroundColor = UIColor.white
button.setTitle("\(i)", for: .normal)
button.setTitleColor(.black, for: .normal)
button.backgroundColor = UIColor.clear
button.addTarget(self, action:#selector(handleRegister(sender:)), for: .touchUpInside)
button.frame = CGRect(x: subVwOffset, y: CGFloat(buttonPadding), width: 200, height: 100)
xOffset = xOffset + CGFloat(buttonPadding) + button.frame.size.width
myNewView.addSubview(button)
}
scView.contentSize = CGSize(width: xOffset, height: scView.frame.height)
Let me know if you find any other difficulty.

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!!!

IOS, Swift: creating a split screen with 2 views, with animation

I'm an absolute IOS newbie studying Swift.
I'm trying to create an app with a calendar UI control on top (already got that working), and a listview on the bottom.
Exactly like the picture on the bottom (and I apologise for the huge pic).
Now, like the calendar app, the screen will only show the calendar control, but if you click a button - the listview will reveal itself on the bottom and the screen will become split.
The only way I can think of doing this is to generate the UITableView in code, assigning the current ViewController as both delegate and datasource, and animating both the views (calendar and listview) to grow/shrink to split the screen.
I don't want to use StackView because that's IOS-9 and up.
Is there a better way to do this?
I suspect you're trying to do something like this. Hope it helps:
Swift 2:
let view2 = UIView()
var screenWidth = CGFloat()
var screenHeight = CGFloat()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
let screenSize: CGRect = UIScreen.mainScreen().bounds
screenWidth = screenSize.width
screenHeight = screenSize.height
let view1 = UIView()
view1.frame = CGRectMake(0, 0, screenWidth, screenWidth)
view1.backgroundColor = UIColor.blueColor().colorWithAlphaComponent(0.5)
self.view.addSubview(view1)
let lbl1 = UILabel()
lbl1.frame = CGRectMake(0, 0, screenWidth, 50)
lbl1.text = "This is view 1"
lbl1.textAlignment = NSTextAlignment.Center
lbl1.center = view1.center
view1.addSubview(lbl1)
let btn = UIButton()
btn.frame = CGRectMake(0,0,200,100)
btn.center = CGPoint(x: view1.center.x, y: view1.center.y+60)
btn.setTitle("Show view 2", forState: .Normal)
btn.addTarget(self, action: "showView2:", forControlEvents: .TouchUpInside)
view1.addSubview(btn)
view2.frame = CGRectMake(0, screenHeight, screenWidth, screenHeight-screenWidth)
view2.backgroundColor = UIColor.redColor().colorWithAlphaComponent(0.5)
self.view.addSubview(view2)
let lbl2 = UILabel()
lbl2.frame = CGRectMake(0, (screenHeight-screenWidth)/2-25, screenWidth, 50)
lbl2.text = "This is view 2"
lbl2.textAlignment = NSTextAlignment.Center
view2.addSubview(lbl2)
}
var c = 0
func showView2(sender : UIButton) {
UIView.animateWithDuration(1.0, delay: 0.0, options: .CurveEaseOut, animations: {
if (self.c%2 == 0) {
self.view2.frame = CGRectMake(0, self.screenWidth, self.screenWidth, self.screenHeight-self.screenWidth)
sender.setTitle("Hide view 2", forState: .Normal)
}
else {
self.view2.frame = CGRectMake(0, self.screenHeight, self.screenWidth, self.screenHeight-self.screenWidth)
sender.setTitle("Show view 2", forState: .Normal)
}
}, completion: { finished in
})
c++
}
Swift 3:
let view2 = UIView()
var screenWidth = CGFloat()
var screenHeight = CGFloat()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
let screenSize: CGRect = UIScreen.main.bounds
screenWidth = screenSize.width
screenHeight = screenSize.height
let view1 = UIView()
view1.frame = CGRect(x: 0, y: 0, width: screenWidth, height: screenWidth)
view1.backgroundColor = UIColor.blue.withAlphaComponent(0.5)
self.view.addSubview(view1)
let lbl1 = UILabel()
lbl1.frame = CGRect(x: 0, y: 0, width: screenWidth, height: 50)
lbl1.text = "This is view 1"
lbl1.textAlignment = NSTextAlignment.center
lbl1.center = view1.center
view1.addSubview(lbl1)
let btn = UIButton()
btn.frame = CGRect(x: 0, y: 0, width: 200, height: 100)
btn.center = CGPoint(x: view1.center.x, y: view1.center.y+60)
btn.setTitle("Show view 2", for: .normal)
btn.addTarget(self, action: #selector(showView2(_:)), for: .touchUpInside)
view1.addSubview(btn)
view2.frame = CGRect(x: 0, y: screenHeight, width: screenWidth, height: screenHeight-screenWidth)
view2.backgroundColor = UIColor.red.withAlphaComponent(0.5)
self.view.addSubview(view2)
let lbl2 = UILabel()
lbl2.frame = CGRect(x: 0, y: (screenHeight-screenWidth)/2-25, width: screenWidth, height: 50)
lbl2.text = "This is view 2"
lbl2.textAlignment = NSTextAlignment.center
view2.addSubview(lbl2)
}
var c = 0
func showView2(_ sender : UIButton) {
UIView.animate(withDuration: 1.0, delay: 0.0, options: .curveEaseOut, animations: {
if (self.c%2 == 0) {
self.view2.frame = CGRect(x: 0, y: self.screenWidth, width: self.screenWidth, height: self.screenHeight-self.screenWidth)
sender.setTitle("Hide view 2", for: .normal)
}
else {
self.view2.frame = CGRect(x: 0, y: self.screenHeight, width: self.screenWidth, height: self.screenHeight-self.screenWidth)
sender.setTitle("Show view 2", for: .normal)
}
}, completion: { finished in
})
c += 1
}

Resources