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

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

Related

How can I always show the UIScrollBar indicator?

When a UIViewController with a UIScrollView is visible on screen, I want to show the UIScrollView scrollbar indicator.
Is there a way to show the indicator?
You can show the scrollbar momentarily with .flashScrollIndicators when the view controller has been added to the view hierarchy and is potentially visible to the user.
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
scrollView.flashScrollIndicators()
}

small navigation title is showing for few seconds

I have set large title in viewWillAppear()
self.navigationItem.largeTitleDisplayMode = .always
self.navigationController?.navigationBar.prefersLargeTitles = true
self.navigationItem.title = "Reports"
But still when I redirect to next VC and come back I can see navigation Title in small size for a while and then I see large title, anybody know why and how to fix it?
If you want the next VC to have largeTitleDisplayMode false, you could try to set it in viewWillDisappear() of the current VC, like this:
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
self.navigationController?.navigationBar.prefersLargeTitles = false
}
You should set this in the viewDidLoad of your view controller, not the viewWillAppear. It's the first part in the view lifecycle and where this work should be done

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

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

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?