Swift Extra argument "width" in call in CGRect - ios

I am trying to add a few buttons in the code
and i get an error "Extra argument "width" in call"
let yPos = txt.frame.origin.y + txt.frame.height
for i in 0..<count {
let yt : Int = i*40 + yPos
//let b = UIButton(frame: CGRect(x: xPos, y: 40, width: 100, height: 30))
var b = UIButton(frame: CGRect(x: xPos, y: yt, width: 100, height: 30))
b.addGestureRecognizer(UITapGestureRecognizer(target: self, action: "test:"))
b.backgroundColor = UIColor.blackColor()
b.tag = i
self.addSubview(b)
}
I cant figure out what is the problem.
I tried to cast all the argument to Int but i get the same error.
Any suggestions?

my mistake i had to cast all the arguments to CGFloat
for i in 0...2 {
let yt : CGFloat = CGFloat(i*40) + yPos
//let b = UIButton(frame: CGRect(x: xPos, y: 40, width: 100, height: 30))
var b = UIButton(frame: CGRect(x: xPos, y: yt, width: 100, height: 30))
b.addGestureRecognizer(UITapGestureRecognizer(target: self, action: "test:"))
b.backgroundColor = UIColor.blackColor()
b.tag = i
self.addSubview(b)
}

Related

Indent UITextField text and placeholder

I have programmatically created a UITextField in my UITableView footer like so:
let customView = UIView(frame: CGRect(x: 0, y: 0, width: self.view.bounds.width, height: 50))
self.textArea = UITextField(frame: CGRect(x: 0, y: 0, width: self.view.bounds.width - 50, height: 50))
self.textArea.placeholder = "Add Item"
self.textArea.layer.borderColor = UIColor.gray.cgColor
self.textArea.layer.borderWidth = 1
customView.addSubview(self.textArea)
let button = UIButton(frame: CGRect(x: self.view.bounds.width - 50, y: 0, width: 50, height: 50))
button.setTitle("Add", for: .normal)
button.setTitleColor(.white, for: .normal)
button.backgroundColor = UIColor(displayP3Red: 3.0 / 255.0, green: 0.0 / 255.0, blue: 113.0 / 255.0, alpha: 1.0)
button.addTarget(self, action: #selector(self.addWishListItem), for: .touchUpInside)
customView.addSubview(button)
self.tableView.tableFooterView = customView
My question is, how do I indent the UITextField text and placeholder text so it's not right at the edge of the left side?
I found this:
let paddingView = UIView(frame: CGRect(x: 0, y: 0, width: 15, height: self.textArea.frame.height))
self.textArea.leftView = paddingView
self.textArea.leftViewMode = .always
Is there a better way on doing this?
You can create a customTextField and override the rects for text, placeholder, border.. basically everything.
import UIKit
class CustomTextField: UITextField {
override func placeholderRect(forBounds bounds: CGRect) -> CGRect {
return CGRect.init(x: 10, y: -10, width: bounds.width, height: bounds.height)
}
override func textRect(forBounds bounds: CGRect) -> CGRect {
return CGRect.init(x: 10, y: -10, width: bounds.width, height: bounds.height)
}
}
You control the position of it by changing x and y values

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.

Converting CGPoints between UIKit and SpriteKit woes: UIViews next to SKNodes

There are many posts on this but for whatever reason I can't seem to get the correct sequence of conversions correct. I'm trying to get the UISliders to go directly below the SKLabels.
As you can see in the pictures, one of the sliders doesn't show at all, and the other one is in the completely wrong place (the volume slider is near the seek label):
Here is my current attempt at getting this right, though I've tried a few other configurations that got me nowhere:
class GameScene: SKScene {
let seekSlider = UISlider(frame: CGRect(x: 0, y: 0, width: 200, height: 15))
let volSlider = UISlider(frame: CGRect(x: 0, y: 0, width: 200, height: 15))
override func didMove(to view: SKView) {
removeAllChildren() // Delete this in your actual project.
// Add some labels:
let seekLabel = SKLabelNode(text: "Seek")
seekLabel.setScale(3)
seekLabel.verticalAlignmentMode = .center
seekLabel.position = CGPoint(x: frame.minX + seekLabel.frame.width/2,
y: frame.maxY - seekLabel.frame.height)
let volLabel = SKLabelNode(text: "Volume")
volLabel.setScale(3)
volLabel.verticalAlignmentMode = .center
volLabel.position = CGPoint(x: frame.minX + volLabel.frame.width/2,
y: frame.minY + volLabel.frame.height + volSlider.frame.height)
/* CONVERSION WOES BELOW: */
// Configure sliders:
let seekOrigin = convertPoint(toView: convert(seekLabel.position, from: self))
seekSlider.frame = CGRect(origin: seekOrigin, size: CGSize(width: 200, height: 15))
seekSlider.value = 1
let volOrigin = convertPoint(fromView: CGPoint(x: volLabel.frame.minX, y: volLabel.frame.minY))
seekSlider.frame = CGRect(origin: volOrigin, size: CGSize(width: 200, height: 15))
seekSlider.value = 0
// Scene stuff:
view.addSubview(seekSlider)
view.addSubview(volSlider)
addChild(seekLabel)
addChild(volLabel)
}
}
You're very close! There are two minor issues in your code:
Issue #1
You modify the seekSlider's frame twice instead of modifying the seekSlider then the volSlider.
let volOrigin = convertPoint(fromView: CGPoint(x: volLabel.frame.minX, y: volLabel.frame.minY))
seekSlider.frame = CGRect(origin: volOrigin, size: CGSize(width: 200, height: 15))
seekSlider.value = 0
should be
let volOrigin = convertPoint(fromView: CGPoint(x: volLabel.frame.minX, y: volLabel.frame.minY))
volSlider.frame = CGRect(origin: volOrigin, size: CGSize(width: 200, height: 15))
volSlider.value = 0
Issue #2
The conversion code is not correct.
Using the convertPoint(toView method with the node's position should do the trick:
let seekOrigin = convertPoint(toView: seekLabel.position)
Final Code:
class GameScene: SKScene {
let seekSlider = UISlider(frame: CGRect(x: 0, y: 0, width: 200, height: 15))
let volSlider = UISlider(frame: CGRect(x: 0, y: 0, width: 200, height: 15))
override func didMove(to view: SKView) {
removeAllChildren() // Delete this in your actual project.
// Add some labels:
let seekLabel = SKLabelNode(text: "Seek")
seekLabel.setScale(3)
seekLabel.verticalAlignmentMode = .center
seekLabel.position = CGPoint(x: frame.minX + seekLabel.frame.width/2,
y: frame.maxY - seekLabel.frame.height)
let volLabel = SKLabelNode(text: "Volume")
volLabel.setScale(3)
volLabel.verticalAlignmentMode = .center
volLabel.position = CGPoint(x: frame.minX + volLabel.frame.width/2,
y: frame.minY + volLabel.frame.height + volSlider.frame.height)
// Configure sliders:
let seekOrigin = convertPoint(toView: seekLabel.position)
let offsetSeekOrigin = CGPoint(x: seekOrigin.x, y: seekOrigin.y + 50)
seekSlider.frame = CGRect(origin: offsetSeekOrigin, size: CGSize(width: 200, height: 15))
seekSlider.value = 1
let volOrigin = convertPoint(toView: volLabel.position)
let offsetVolOrigin = CGPoint(x: volOrigin.x, y: volOrigin.y - 50)
volSlider.frame = CGRect(origin: offsetVolOrigin, size: CGSize(width: 200, height: 15))
volSlider.value = 0
// Scene stuff:
view.addSubview(seekSlider)
view.addSubview(volSlider)
addChild(seekLabel)
addChild(volLabel)
}
}
Final Result:

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

Remove subview created for activityIndicator with swift

i have created an activity window function which takes 3 inputs,
msg:String - the message which should show in the activity window
indicator:Bool - if the activity window should show or not
view:UIView - the uiview which should get the frame sizes etc from the View Controller which its called on
everything works fine, except the part where subview needs to be removed.if the same function is run on the main view controller. it works fine. just when i moved it to NSObject, it does not. please help
class UIDesignFunction: NSObject {
func progressBarDisplayer(msg:String, indicator:Bool, view:UIView) {
var activityIndicator = UIActivityIndicatorView()
var strLabel = UILabel()
var msgFrame = UIView()
if indicator{
//println(msg)
strLabel = UILabel(frame: CGRect(x: 50, y: 0, width: 200, height: 50))
strLabel.text = msg
strLabel.textColor = UIColor.whiteColor()
msgFrame = UIView(frame: CGRect(x: view.frame.midX - 90, y: view.frame.midY - 25 , width: 180, height: 50))
msgFrame.layer.cornerRadius = 15
msgFrame.backgroundColor = UIColor(white: 0, alpha: 0.7)
activityIndicator = UIActivityIndicatorView(activityIndicatorStyle: UIActivityIndicatorViewStyle.White)
activityIndicator.frame = CGRect(x: 0, y: 0, width: 50, height: 50)
activityIndicator.startAnimating()
msgFrame.addSubview(activityIndicator)
UIApplication.sharedApplication().beginIgnoringInteractionEvents()
msgFrame.addSubview(strLabel)
view.addSubview(msgFrame)
}else{
UIApplication.sharedApplication().endIgnoringInteractionEvents()
msgFrame.removeFromSuperview()
}
}
move the variables out of the function like below
class UIDesignFunction: NSObject {
var activityIndicator = UIActivityIndicatorView()
var strLabel = UILabel()
var msgFrame = UIView()
func progressBarDisplayer(msg:String, indicator:Bool, view:UIView) {
if indicator{
//println(msg)
strLabel = UILabel(frame: CGRect(x: 50, y: 0, width: 200, height: 50))
strLabel.text = msg
strLabel.textColor = UIColor.whiteColor()
msgFrame = UIView(frame: CGRect(x: view.frame.midX - 90, y: view.frame.midY - 25 , width: 180, height: 50))
msgFrame.layer.cornerRadius = 15
msgFrame.backgroundColor = UIColor(white: 0, alpha: 0.7)
activityIndicator = UIActivityIndicatorView(activityIndicatorStyle: UIActivityIndicatorViewStyle.White)
activityIndicator.frame = CGRect(x: 0, y: 0, width: 50, height: 50)
activityIndicator.startAnimating()
msgFrame.addSubview(activityIndicator)
UIApplication.sharedApplication().beginIgnoringInteractionEvents()
msgFrame.addSubview(strLabel)
view.addSubview(msgFrame)
}else{
UIApplication.sharedApplication().endIgnoringInteractionEvents()
msgFrame.removeFromSuperview()
}
}

Resources