SWIFT: UIImageView Photo Going Past Boundaries - swift

Can anybody tell me why my photos go beyond the boundary of UIImageView?
The pictures end up covering the buttons below it.
My code:
import UIKit
class ViewController: UIViewController {
#IBOutlet var Photo: UIImageView!
#IBOutlet var QuestionLabel: UILabel!
#IBOutlet var Button1: UIButton!
#IBOutlet var Button2: UIButton!
#IBOutlet var Button3: UIButton!
#IBOutlet var Button4: UIButton!
#IBOutlet var LabelEnd: UILabel!
var CorrectAnswer = String()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
Hide()
Photo.layer.cornerRadius = self.Photo.frame.size.width / 10
Photo.clipsToBounds = true
Photo.layer.masksToBounds = true
Photo.layer.borderWidth = 1
Photo.layer.borderColor = UIColor.blackColor().CGColor
Photo.contentMode = .ScaleAspectFit
Photo.contentMode = .ScaleAspectFill
RandomQuestions()
}
I apologize for being such a novice, just learning how to code.

Are you sure it's going beyond the boundary of your image view?
Try printing out the frame of your view to double check. Doesn't look like you have constraints set up from that screenshot - maybe your image view is sized to fit.

Related

The picture goes up because the description is long how can I fix this?

When the picture description is long, my picture disappears, I can't see it. How can I show full size of text and full size of images?
How can I scroll the text up and down?
FavoriteDetailViewController
import UIKit
import ProgressHUD
class FavoriteDetailViewController: UIViewController {
#IBOutlet weak var shortdescriptionLabel: UILabel!
#IBOutlet weak var favoriteImageView: UIImageView!
#IBOutlet weak var favoriteTitleLabel: UILabel!
#IBOutlet weak var descriptionLabel: UILabel!
#IBOutlet weak var nameField: UITextField!
var breed: Breed!
override func viewDidLoad() {
super.viewDidLoad()
populateView()
}
private func populateView() {
favoriteImageView.kf.setImage(with: breed.image?.asUrl)
favoriteTitleLabel.text = breed.name
descriptionLabel.text = breed.description
shortdescriptionLabel.text = breed.shortDescription
}
#IBAction func favoriteButton(_ sender: UIButton) {
guard let name = nameField.text?.trimmingCharacters(in: .whitespaces),
!name.isEmpty else {
ProgressHUD.showError("Please enter your name")
return
}
print("Hello \(name)")
}
}
I made the text smaller but it's very illegible
I dropped the spacing, it looks bad too
I want to make the whole text look nice and not spoil the picture. What is the solution? Is it possible to move the label up and down?

How to resize the detailsView after cross button is clicked after the closing view is hidden?

[![when the cross button is clicked, the closingView is hidden.][1]][1]
import UIKit
import IBAnimatable
class ViewController: UIViewController {
#IBOutlet weak var closingView: AnimatableView!
#IBOutlet weak var detailsView: AnimatableView!
#IBOutlet weak var btnClose: UIButton!
#IBOutlet weak var closeViewBtn: UIView!
#IBOutlet weak var scrollView: UIScrollView!
var isCloseButtonTapped = false
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
#IBAction func btnCloseAction(_ sender: Any) {
isCloseButtonTapped = !isCloseButtonTapped
if isCloseButtonTapped{
closingView.isHidden = true
}else{
//
}
}
}
[1]: https://i.stack.imgur.com/CpH2A.png
You can add closingView and detailsView inside a UIStackView. When the closingView is hidden the detailsView will fill up the entire space in the parent UIStackView.

Variable UILabel name

Is it possible to make a label as a variable? I have multiple labels:
#IBOutlet weak var FL1at0: UIButton!
#IBOutlet weak var FL1at3: UIButton!
#IBOutlet weak var FL1at6: UIButton!
#IBOutlet weak var FL1at9: UIButton!
#IBOutlet weak var FL1at12: UIButton!
#IBOutlet weak var FL1at15: UIButton!
#IBOutlet weak var FL1at18: UIButton!
#IBOutlet weak var FL1at21: UIButton!
Sample Function:
else
{
UserDefaults.standard.set(true, forKey: "RedFl1")
self.FL1at0.setTitleColor(UIColor.red, for: .normal); self.ProgressUpdate()
}
Is there a way to set the table in a way to work like this:
self.FL1at"\(TimeStamp)".setTitleColor(UIColor.red, for: .normal)//TimeStamp comes from function
Try this:
let dict: Dictionary<String, UIButton> = ["FL1at0": FL1at0, "FL1at3": FL1at3, ...]
let x = "FL1at\(TimeStamp)"
if let y = dict[x] as? UIButton {
y.setTitleColor(UIColor.red, for: .normal)
}
You can't.
But you can save buttons in a dictionary:
var dic: [String: UIButton] = ["FL1at0": FL1at0]
then you can use name to access it like:
dic["FL1at\(TimeStamp)"]?.setTitleColorxxxx..

How can I carry out multiple different animations at different times with the same UIButton?

I am creating an animation sequence where when one button is pressed one object fades out. When the same button is pressed again another object fades out. This keeps happening until 8 objects are faded out. I have tried and played around with code and so far have got the code to fade out one object, and two or more at a time, but have not successfully been able to do it as described above. This is my code so far:
import UIKit
class DrinkWaterViewController: UIViewController {
#IBOutlet weak var glassOne: UIImageView!
#IBOutlet weak var glassTwo: UIImageView!
#IBOutlet weak var glassThree: UIImageView!
#IBOutlet weak var glassFour: UIImageView!
#IBOutlet weak var glassFive: UIImageView!
#IBOutlet weak var glassSix: UIImageView!
#IBOutlet weak var glassSeven: UIImageView!
#IBOutlet weak var glassEight: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
self.glassOne.alpha = 1.0
self.glassTwo.alpha = 1.0
}
#IBAction func drunkOne(_ sender: Any){
if self.glassOne.alpha == 1.0{
UIView.animate(withDuration: 1.5, delay: 0.2, animations:
{self.glassOne.alpha = CGFloat(0.1)}
)
}
}
Put your glasses into an array glasses and add a property to keep track of nextToFade. Then just fade the glasses[nextToFade] and increment nextToFade by 1:
class DrinkWaterViewController: UIViewController {
#IBOutlet weak var glassOne: UIImageView!
#IBOutlet weak var glassTwo: UIImageView!
#IBOutlet weak var glassThree: UIImageView!
#IBOutlet weak var glassFour: UIImageView!
#IBOutlet weak var glassFive: UIImageView!
#IBOutlet weak var glassSix: UIImageView!
#IBOutlet weak var glassSeven: UIImageView!
#IBOutlet weak var glassEight: UIImageView!
// array to hold the glasses so that they can be indexed
var glasses = [UIImageView]()
// index of next glass to fade
var nextToFade = 0
override func viewDidLoad() {
super.viewDidLoad()
// put the glasses into the array
glasses = [glassOne, glassTwo, glassThree, glassFour,
glassFive, glassSix, glassSeven, glassEight]
// make sure they're all visible
glasses.forEach { $0.alpha = 1.0 }
}
#IBAction func drunkOne(_ sender: Any) {
// make sure not to index beyond the end of the array
guard nextToFade < glasses.count else { return }
let glass = glasses[nextToFade]
nextToFade += 1
UIView.animate(withDuration: 1.5, delay: 0.2, animations:
{ glass.alpha = 0.1 }
)
}
}

Why isn't this code in ViewDidLoad executing?

I have two text fields, one button, and four labels.
When you push the button, it should update the four labels.
Label 1: name
Label 2: email
Label 3: name (same data as label 1)
Label 4: email (same data as label 2)
I was experimenting under viewDidLoad() to see if I can make another var and update my last label. No error when I run the code, but it turns out "empty" in the simulator.
Can anyone explain why?
import UIKit
class ViewController: UIViewController {
#IBOutlet weak var emailInput: UITextField!
#IBOutlet weak var nameInput: UITextField!
#IBOutlet weak var label: UILabel!
#IBOutlet weak var label2: UILabel!
#IBOutlet weak var label3: UILabel!
#IBOutlet weak var label4: UILabel!
#IBAction func actionButton(_ sender: Any) {
var text = nameInput.text
var email = emailInput.text
label.text = text
label2.text = email
label3.text = text
}
//where i experimented:
override func viewDidLoad() {
super.viewDidLoad()
var email2 = emailInput.text
label4.text = email2
}
}
Change
label4.text = email2
To
label4.text = "howdy"
Now you will see something in your interface.
The viewDidLoad method called in the beginning perfectly. But the label not showing any data because initially the textfield has no value. That's the label not contains any data.
Please set any value to the label initially. Like
label4.text = "Something"
Hope you can understand that the viewDidLoad is called perfectly.