Swift - If statement not working as it should - ios

I'm trying to animate an UIButton once two textsfields have had text entered. The button animates even if there is no text entered. I thought maybe it was an issue with there being placeholder text, but that has no effect. I have tried != "" as well as != nil
Here's my code
override func viewDidLoad() {
super.viewDidLoad()
self.title = ""
textFieldConfig()
loginButtonConfig("LOG IN")
}
func loginButtonConfig(title:String) {
let frameWidth = self.view.frame.width
let frameHeight = self.view.frame.height
var button = UIButton()
button.frame = CGRect(x: 0, y: 300, width: 0, height: 50)
button.bounds = CGRect(x: 0, y: 0, width: 0, height: 50)
button.backgroundColor = UIColor(red: 24.0/255.0, green: 198.0/255.0, blue: 152.0/255.0, alpha: 1.0)
button.setTitle(title, forState: .Normal)
button.titleLabel?.textAlignment = NSTextAlignment.Center
self.view.addSubview(button)
//WHY ISN'T THE IF STATEMENT WORKING AS IT SHOULD???
if self.userTextField.text != nil && self.passwordTextField.text != nil {
UIView.animateWithDuration(1.0, animations: { () -> Void in
button.frame = CGRect(x: 0, y: 300, width: frameWidth, height: 50)
button.bounds = CGRect(x: 0, y: 0, width: frameWidth, height: 50)
})
}
}
Any help would be appreciated. Thanks :)

Try this:
if !self.userTextField.text.isEmpty && !self.passwordTextField.isEmpty {
UIView.animateWithDuration(1.0, animations: { () -> Void in
button.frame = CGRect(x: 0, y: 300, width: frameWidth, height: 50)
button.bounds = CGRect(x: 0, y: 0, width: frameWidth, height: 50)
})
}

Try this:
if self.userTextField.text! != "" && self.passwordTextField.text! != "" {
UIView.animateWithDuration(1.0, animations: { () -> Void in
button.frame = CGRect(x: 0, y: 300, width: frameWidth, height: 50)
button.bounds = CGRect(x: 0, y: 0, width: frameWidth, height: 50)
})
}

Related

Set section header padding of tableview for second header in old iOS

I'm setting header padding for my tableview in iOS older than 15 explicitly. This works for the top first header (section 1 of tableview), but doesn't work for second header (section 2 of tableview). How could I set it for the second also?
Is there something like tableView.tableHeaderView[2] or tableView.tableHeaderView.second ?
In code its the place with commented-out places.
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
if tableView.tag == 2 {
if section == 0 {
if self.tableView(tableView, numberOfRowsInSection: section) > 0 {
let view = UIView()
let imageView = UIImageView(image: UIImage(named: "Logo_face"))
if #unavailable(iOS 15.0) {
imageView.frame = CGRect(x: 10, y: 20, width: 25, height: 25)
} else {
imageView.frame = CGRect(x: 10, y: 0, width: 25, height: 25)
}
view.addSubview(imageView)
let label = UILabel()
label.text = "店舗"
label.frame = CGRect(x: 40, y: 0, width: 200, height: 25)
view.addSubview(label)
if #unavailable(iOS 15.0) { //setting here; works
let padding: CGFloat = 20
view.frame = CGRect(x: view.frame.origin.x - padding,
y: view.frame.origin.y - padding,
width: view.frame.width + 2 * padding,
height: view.frame.height + 2 * padding)
tableView.tableHeaderView = view // this is good
label.frame = CGRect(x: 40, y: 20, width: 200, height: 25)
}
return view
}
} else {
if self.tableView(tableView, numberOfRowsInSection: section) > 0 {
let view = UIView()
let imageView = UIImageView(image: UIImage(named: "pinIconForSearch"))
if #unavailable(iOS 15.0) {
imageView.frame = CGRect(x: 10, y: 20, width: 25, height: 25)
} else {
imageView.frame = CGRect(x: 10, y: 0, width: 25, height: 25)
}
view.addSubview(imageView)
let label = UILabel()
label.text = "検索候補"
label.frame = CGRect(x: 40, y: 0, width: 200, height: 25)
view.addSubview(label)
if #unavailable(iOS 15.0) { //setting here doesnt work
let padding: CGFloat = 20
view.frame = CGRect(x: view.frame.origin.x - padding,
y: view.frame.origin.y - padding,
width: view.frame.width + 2 * padding,
height: view.frame.height + 2 * padding)
tableView.tableHeaderView = view // this is not good
label.frame = CGRect(x: 40, y: 20, width: 200, height: 25)
}
return view
}
}
}
if #available(iOS 15.0, *) {
tableView.sectionHeaderTopPadding = 0.0
} else {}
return nil
}

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

Moving UIImage Up and Down with Animation in Swift

I have just recently started with Xcode and Swift and I am trying to get my image to move up and down and have it switch directions when the user taps the screen. My image just will not move at all and the app will either crash or just freeze.
import UIKit
import SpriteKit
import Darwin
let startButton = UIButton(frame: CGRect(x: 200, y: 100, width: 100, height: 50))
let screenSize = UIScreen.main.bounds
let gameButton = UIButton(frame: CGRect(x: 0, y: 0, width: screenSize.width, height: screenSize.height))
var dot: UIImage = UIImage(named: "dot")!
var dotPic = UIImageView(image: dot)
public var goingUp = true
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
self.view.backgroundColor = .white
startButton.addTarget(self, action: #selector(startButtonAction), for: .touchUpInside)
startButton.setImage(UIImage.init(named: "startButton"), for: .normal)
self.view.addSubview(startButton)
}
func startButtonAction(sender: UIButton!) {
print("button clicked")
startButton.isEnabled = false
startButton.setImage(UIImage.init(named: "nothing"), for: .normal)
self.view.backgroundColor = .gray
dotPic.frame = CGRect(x: 50, y: 300, width: 20, height: 20)
self.view.addSubview(dotPic)
self.view.addSubview(gameButton)
playGame()
}
func playGame(){
gameButton.addTarget(self, action: #selector(gameButtonAction), for: .touchUpInside)
gameButton.setImage(UIImage.init(named: "nothing"), for: .normal)
}
func gameButtonAction(sender: UIButton!) {
goingUp = !goingUp
print(goingUp)
print("clicked up/down")
func rep(){
repeat{
dotPic.frame = CGRect(x: 50, y: 300-1, width: 20, height: 20)
sleep(UInt32(0.3))
}while goingUp==true
repeat{
dotPic.frame = CGRect(x: 50, y: 300-1, width: 20, height: 20)
sleep(UInt32(0.3))
}while goingUp==false
}
rep()
}
}
I have tried using animateWithDuration() too but it has always frozen the app. What am I doing wrong?
Tried to use this code in swift...
self.hintImageView.frame = CGRect(x: x, y: 55, width: 200, height: 40)
UIView.animate(withDuration: 0.5, delay: 0, options: [.repeat, .autoreverse] , animations: {
self.hintImageView.frame = CGRect(x: x, y: 65, width: 200, height: 40);
}) { (completed) in
}
Here, Try this, instead of func rep() in your gameButtonAction :
[UIView animateWithDuration:duration delay:0 options:UIViewAnimationOptionCurveLinear animations:^{
//code with animation
dotPic.frame = CGRect(x: 50, y: 300-1, width: 20, height: 20)
} completion:^(BOOL finished) {
//code for completion
dotPic.frame = CGRect(x: 50, y: 300, width: 20, height: 20)
}];
and avoid using nested function rep() in your target. Probably it is running continuously causing your application to hang.

How to animate UIButton frame from left to right and reverse

My current method is to add a view to the button as subview:
let button = UIButton(frame: CGRect(x: 0, y: 0, width: 0, height: 60))
button.setTitle("Add", for: .normal)
buttonView = UIView(frame: CGRect(x: 0, y: 0, width: 0, height: 60))
buttonView.backgroundColor = .black
button.addSubview(buttonView)
And then animate the width of the view:
UIView.animate(withDuration: 0.3, delay: 0.0, options: .curveEaseIn, animations: {
self.buttonView.frame = CGRect(x: 0, y: 0, width: self.view.frame.size.width, height: 60)
}, completion: nil)
What I don't like about this is that the button title remains static. I want it to be part of the animation.
I also try to animate the frame of the button instead, but it doesn't seem to work. Nothing happens.
My button is added to the UITextField inputAccessoryView so I am not sure if this is the problem.
Any suggestions how can I animate the button correctly ?
You don't need an additional subview for UIButton in order to animate it's frame.
Try to add layoutIfNeeded() before animation and inside animation closure:
let button = UIButton(frame: CGRect(x: 0, y: 0, width: 60, height: 60))
button.setTitle("Add", for: .normal)
button.setTitleColor(UIColor.green, for: .normal)
button.backgroundColor = UIColor.black
view.addSubview(button)
button.layoutIfNeeded()
UIView.animate(withDuration: 3, delay: 0.0, options: .curveEaseIn, animations: {
button.frame = CGRect(x: 0, y: 0, width: self.view.frame.size.width, height: 60)
button.layoutIfNeeded()
}, completion: nil)
Hope this helps.

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