Swift 2: problems with UILabel position after the view is loaded - swift

I have a little problem with an ULlabel. All I want to do is to move a UIlabel outside the view's bounds before it loads and then, when the view is loaded, show with a little animation.
So I have done this:
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
self.lblQuestion.hidden = true
}
override func viewDidAppear(animated: Bool) {
super.viewDidAppear(animated)
self.lblQuestion.center.y = -(self.view.bounds.height + 100)
}
It seems to be all ok because if I put a print("\\(self.lblQuestion.center.y)") in the viewDidAppear function I can see that the position is ok and the UIlabel is outside the bounds, like I want.
But if I wait 2 seconds and check with a new print("\\(self.lblQuestion.center.y)") inside a timer (for example) I see that the y position of the UIlabel is changed.
Does the UIView change its bounds after some event?
Can someone tell me what is happening, please?

Related

I can't get UITextView to scroll to its bottom before UIViewController appears

I have a UIViewText that I have in all my UIViewControllers that carries over data from the entire app's instance.
If I put the 2 lines that scroll to the bottom in the viewDidAppear section, the text scrolls to the bottom, but you see it occur, so it's not pleasant visually.
However, if I put the same 2 lines in the viewWillAppear section (as shown below) then for some reason the UITextView starts at the top of the text.
Am I somehow doing this incorrectly?
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
myStatusWin.text = db.status
let range = NSMakeRange(myStatusWin.text.count - 1, 0)
myStatusWin.scrollRangeToVisible(range)
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
}
p.s. I have posted this question here, https://developer.apple.com/forums/thread/715382, but no responses

When I go back to a view controller after opening it previously it does not animate labels Xcode Swift

When I open my app I have this label that animates in from outside the screen. When I open the view controller for the first time it works. However, when I go to another view controller and then go back to the initial view controller this label will just be there and not animate in.
levelsLabel.center = CGPoint(x:levelsLabel.center.x - 500, y:levelsLabel.center.y)
UIView.animate(withDuration: 2) {
self.levelsLabel.center = CGPoint(x:self.levelsLabel.center.x + 500, y:self.levelsLabel.center.y)
}
Anyone have any suggestions?? Thank You!
Putting your block of code in viewWillAppear rather than viewDidLoad will make it work. However, according to Apple Doc.
viewDidAppear: Use this method to trigger any operations that need
to occur as soon as the view is presented onscreen, such as fetching
data or showing an animation.
The following is what I'd recommend you to do.
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
levelsLabel.center = CGPoint(x:levelsLabel.center.x - 500, y:levelsLabel.center.y)
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
UIView.animate(withDuration: 2) {
self.levelsLabel.center = CGPoint(x:self.levelsLabel.center.x + 500, y:self.levelsLabel.center.y)
}
}

SIGABRT when adding a constraint to a uiview

Once again auto layout has me scratching my head. I define a UIView and UITextField at the top of my class:
var urlField = UITextField(frame: .zero)
var barView = UIView(frame: .zero)
I call my configuration method from viewWillAppear (so I know that self.view is all set):
override func viewWillAppear(_ animated: Bool) {
configureURLBar()
}
And I'm doing nothing special there.
func configureURLBar() {
barView.translatesAutoresizingMaskIntoConstraints = false
barView.topAnchor.constraint(equalTo: self.view.layoutMarginsGuide.topAnchor).isActive = true
barView.heightAnchor.constraint(equalToConstant: 30).isActive = true
}
When it gets to the topAnchor constraint I get the exception, and I don't understand why. If I reverse the 2nd and 3rd lines it doesn't blow up until it gets to the .topAnchor line, so that's the problem. I can ask a second question once I understand the reason for this exception (in case you're wondering what I'm trying to do): how do I add a user input (url) bar at the top of my UIWebView. (that didn't work either - same results if I try to constrain against my self.webView which is displaying perfectly) Also: I call self.view.setNeedsDisplay() before viewWillAppear() is called.
You have to add your view to parent view first. After that you can add constraints.
Try this:
override func viewWillAppear(_ animated: Bool)
{
self.view.addSubview(barView)
configureURLBar()
}
Include your logic in viewDidLayoutSubviews instead of in viewWillAppear...
viewDidLayoutSubviews is a much better place to handle geometry

How to simply animate an UIimageView?

I want to move a 40, 40 pixel square from the top to the bottom, then repeat that action over a period of 3 seconds. Where would I put the entire thing in the viewcontroller? There is no start button so would I put in in the viewdidload?
So far I have this
UIView.animateWithDuration(0.2, animations: {
}, completion: {
})
As somewhere to start. Would I set the image to equal UIView? What should It look like? the image is called mrock.
I tried using blank.moveToPoint(CGPoint(x: 0,y: 0)) in the animations part, but dont I need A UIBezierPath thing?
As #picciano stated you should have an IBOutlet to an UIImageView if you are using storyboards. So you in your view controller you can try something like this:
#IBOutlet weak var imageView: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
//'img' is a picture imported in your project
self.imageView.image = UIImage(named: "img")
}
override func viewDidAppear(animated: Bool) {
super.viewDidAppear(animated)
let newPos: CGFloat = 300.0
UIView.animateWithDuration(1.0, animations: { () -> Void in
self.imageView.frame = CGRectMake(0, newPos, CGFloat(self.imageView.bounds.size.width), CGFloat(self.imageView.bounds.size.height))
})
}
First, consider putting the animation in viewWillAppear, then disable it if needed in viewDidDisappear.
Second, if you're using auto layout, you will probably need to create an IBOutlet for the relevant constraint and animate the constant property of the constraint rather than the position of the view object directly.

Scroll Position of UITextView at the top

I am using swift and want a UITextView to be at the top when the view launches. At the moment when I launch the app the UITextView is scrolled to the end. I have tried looking online and think scrollRangeToVisible might work but do not know how to use it in swift.
import UIKit
class ThirdViewController: UIViewController {
#IBOutlet weak var FunFact: UITextView!
override func viewDidLoad() {
super.viewDidLoad()
FunFact.scrollRangeToVisible(0, 0)
// Do any additional setup after loading the view.
}
}
Add this to your ViewController:
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
textView.layoutIfNeeded()
textView.contentOffset = CGPoint.zero
}
This scrolls the UITextView to the top. Since it only knows its size, when it is layouted, "layoutIfNeeded" is needed after you changed the text (i changed it in viewDidLoad).
Try this:
var zeroOffset = CGPoint.zeroPoint
FunFact.contentOffset(zeroOffset)
This should bring the offset to 0 (the offset is an indication of how far the current position is from the initial one)