I have a datePicker that I want to animate in and out of view from the bottom by changing its' top constraint to the super view top.
I have an IBOutlet set to it and on viewDidLoad I'm allowed to change the constraint constant.
override func viewDidLoad() {
super.viewDidLoad()
self.datePickerTopConstraint.constant = self.view.frame.size.height // I can set this to whatever and it will persist
}
However via an IBAction I try to set the constant to another value and that doesn't persist.
#IBAction func showDatePicker() {
UIView.animateWithDuration(0.25, animations: {
() -> Void in
self.datePickerTopConstraint.constant = self.view.frame.size.height - self.datePicker.frame.size.height // Doesn't persist
self.view.layoutIfNeeded()
})
}
It seems that I can reverse this and have the datePicker appear in view (in viewDidLoad) and animate it out of view, but not have the datePicker appear out of view (like in the example above) and animate inside the view. What have I missed?
EDIT
By setting the top constraint constant to the super view's height I (for some reason I don't understand) also set the date picker's height to 0 which in turn makes the subtraction in showDatePicker pointless.
Restructure the code so that in the button's method work's by first working out the constant's new value and then calling the animation. Pull the height calculation in to it's own function. I think that self.datePicker.frame.size.height does not exist and results in 0, but I would check this using the debugger.
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
datePickerTopConstraint.constant = constantForDatePickerViewHeightConstraint()
view.setNeedsLayout()
}
#IBAction func showDatePicker(button: UIButton) {
// Check constraint constant
if datePickerTopConstraint.constant == self.view.frame.size.height {
// Date picker is NOT visible
datePickerTopConstraint.constant = constantForDatePickerViewHeightConstraint()
} else {
// Date picker is visible
datePickerTopConstraint.constant = self.view.frame.size.height
}
UIView.animateWithDuration(0.25,
animations: {() -> Void in
self.view.layoutIfNeeded()
})
}
private func constantForDateOPickerViewHeightConstraint() -> CGFloat {
var value : CGFloat = 200.0
// Workout value you want to as the constant for the constraint.
return value
}
Try this:
func showDatePicker() {
self.view.layoutIfNeeded()
UIView.animateWithDuration(0.25, animations: {
() -> Void in
self.datePickerTopConstraint.constant = self.view.frame.size.height - self.datePicker.frame.size.height // Doesn't persist
self.view.layoutIfNeeded()
})
}
You need to call layoutIfNeeded before the animation bock and in the block. Now it is calculating the views correctly. Like I said there is also no point in having any constraints set in the viewDidLoad, if you are going to do it anywhere do it in viewWillAppear. The view hasn't finished setting up in viewDidLoad so there is no constraints available to set properly. Calling layoutIfNeeded before the animation block fixes this mistake and you need it there anyway so it can calculate correctly in the future too.
Related
I have to move the UIView in only last UITextField in Swift 3.0 on mentioned below delegate method using tag,
func textFieldDidBeginEditing(_ textField: UITextField) {
if (textField.tag == 4){
//UIView Up
}
}
func textFieldShouldEndEditing(_ textField: UITextField) -> Bool {
if (textField.tag == 4){
//UIView Down
}
return true
}
I tried many codes but none of them are working like notification,..etc.
You need to add Observers into the NotificationCenter for listening to both when Keyboard goes up and down (i'll assume your textfield outlet is lastTextField for this example to work but this obviously have to be adapted to whatever name you've had provide for it)
IBOutlet weak var passwordTextField: UITextField!
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow(_:)), name: .UIKeyboardWillShow, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillHide(_:)), name: .UIKeyboardWillHide, object: nil)
(Code above can be added in viewDidLoad())
Then you add methods to be executed when those notifications arrive, like this:
func keyboardWillShow(_ notification:Notification) {
if view.frame.origin.y >= 0 && lastTextField.isFirstResponder {
view.frame.origin.y -= getKeyboardHeight(notification)
}
}
func keyboardWillHide(_ notification:Notification) {
if view.frame.origin.y < 0 {
view.frame.origin.y += getKeyboardHeight(notification)
}
}
Validations within those methods prevent double execution like moving up/down twice when moving between textfields without resigning first responder which is common in cases like your (i assume your doing this for a form hence the clarification you only need it for the fourth textfield). Notice i'm only doing validation in for the specified textfield (with its outlet lastTextField) in the keyboardWillShow method, this in case you move thor another textfield while the keyboard is shown and resign responder from it in which case, even though it isn't the original place where you started, the view will return to its original place when the keyboard is hidden.
You'll also need a method for getting keyboard's height, this one can help with that:
func getKeyboardHeight(_ notification:Notification) -> CGFloat {
let userInfo = notification.userInfo
let keyboardSize = userInfo![UIKeyboardFrameEndUserInfoKey] as! NSValue // of CGRect
return keyboardSize.cgRectValue.height
}
Let me know how it goes but i just tested this same code on my app and it works so you should be fine.
PS: pay close attention to the storyboard (if you're using it) and that delegate for textfields are set up properly.
The problem you are trying to remedy is rather complicated, because it requires you to:
Find the textField which is firstResponder
Calculate where that TextField is relative to it's superViews
Determine the distance for the animation, so that the containing
superview doesnt jump out of the window, or jumps too
much/repeatedly
Animate the proper superView.
As you can see.. it's quite the algorithm. But luckily, I can help. However, this only works for a hierarchy which has the following layout:
superView (view in the case of UIViewController) > (N)containerSubviews > textFields
where N is an integer
or the following:
superView (view in the case of UIViewController) > textFields
The idea is to animate superView, based on which textField is firstResponser, and to calculate if it's position inside of the SCREEN implies that it either partially/totally obstructed by the Keyboard or that it is not positioned the way you want for editing. The advantage to this, over simply moving up the superView when the keyboard is shown in an arbitrary manner, is that your textField might not be positioned properly (ie; obstructed by the statusbar), and in the case where your textfields are in a ScrollView/TableView or CollectionView, you can simply scroll the texfield into the place you want instead. This allows you to compute that desired location.
First you need a method which will parse through a given superView, and look for which of it's subViews isFirstResponder:
func findActiveTextField(subviews : [UIView], textField : inout UITextField?) {
for view in subviews {
if let tf = view as? UITextField {
guard !tf.isFirstResponder else {
textField = tf; break
return
}
} else if !subviews.isEmpty {
findActiveTextField(subviews: view.subviews, textField: &textField)
}
}
}
Second, to aleviate the notification method, also make a method to manage the actual animation:
func moveFromDisplace(view: UIView, keyboardheight: CGFloat, comp: #escaping (()->())) {
//You check to see if the view passed is a textField.
if let texty = view as? UITextField {
//Ideally, you set some variables to animate with.
//Next step, you determine which textField you're using.
if texty == YourTextFieldA {
UIView.animate(withDuration: 0.5, animations: {
self./*the proper superView*/.center.y = //The value needed
})
comp()
return
}
if texty == YourTextFieldB {
// Now, since you know which textField is FirstResponder, you can calculate for each textField, if they will be cropped out by the keyboard or not, and to animate the main view up accordingly, or not if the textField is visible at the time the keyboard is called.
UIView.animate(withDuration: 0.5, animations: {
self./*the proper superView*/.center.y = //The Value needed
})
comp()
return
}
}
}
Finally, the method which is tied to the notification for the keyboardWillShow key; in this case, i have a UIViewController, with an optional view called profileFlow containing a bunch of UITextFields
func searchDisplace(notification: NSNotification) {
guard let userInfo:NSDictionary = notification.userInfo as NSDictionary else { return }
guard let keyboardFrame:NSValue = userInfo.value(forKey: UIKeyboardFrameEndUserInfoKey) as? NSValue else { return }
let keyboardRectangle = keyboardFrame.cgRectValue
let keyboardHeight = keyboardRectangle.height
let keybheight = keyboardHeight
var texty : UITextField? //Here is the potential textfield
var search : UISearchBar? //In my case, i also look out for searchBars.. So ignore this.
guard let logProfile = profileFlow else { return }
findActiveTextField(subviews: [logProfile], textField: &texty)
//Check if the parsing method found anything...
guard let text = texty else {
//Found something.. so determine if it should be animated..
moveFromDisplace(view: searchy, keybheight: keybheight, comp: {
value in
search = nil
})
return
}
//Didn't find anything..
}
Finally, you tie in this whole logic to the notification:
NotificationCenter.default.addObserver(self, selector: #selector(searchDisplace(notification:)), name: NSNotification.Name.UIKeyboardWillShow, object: nil)
I can't provide more content to the code, since it all depends on your view hierarchy, and how you want things to animate. So it's up to you to figure that out.
On a side note, usually, if you have so many textfields that to lay them out properly means they overstep the length of the screen.. it's probable that you could simplify your layout. A way to make this algorithm better would be to make sure you have all your textfields in one containing view, which again can become heavy for when, say, you use AutoLayout constraints. Odds are if you're in this situation, you can probably afford to add a flow of several views etc.
There is also the fact that i've never really needed to use this for iPhone views, more for iPad views, and even then for large forms only (e-commerce). So perhaps if you're not in that category, it might be worth reviewing your layout.
Another approach to this, is to use my approach, but to instead check for specific textFields right in the findActiveTextField() method if you only have a handful of textfields, and to animate things within findActiveTextField() as well if you know all of the possible positions they can be in.
Either way, i use inout parameters in this case, something worth looking into if you ask me.
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?
In my tvOS app I have a TabBarController with 3 viewControllers. What I want to do is to automatically hide/change focus of the tabBar when I switch to the next viewController.
I saw some posts here, on SO that suggested to change alfa on the tabBar, but I would like to have a slide up animation, same way as it does when you change focus to something in the viewController.
Any kind of help is highly appreciated.
As Charles said.. Something like this in the derived UITabBarController:
var focusOnChildVC : Bool = false {
didSet {
self.setNeedsFocusUpdate()
}
};
override weak var preferredFocusedView: UIView? {
get {
let v : UIView?;
let focused = UIScreen.mainScreen().focusedView
//A bit of a hack but seems to work for picking up whether the VC is active or not
if (focusOnChildVC && focused != nil) {
v = self.selectedViewController?.preferredFocusedView
} else {
//If we are focused on the main VC and then clear out of property as we're done with overriding the focus now
if (focusOnChildVC) {
focusOnChildVC = false
}
v = super.preferredFocusedView;
}
return v
}
}
The basic idea of the solution described below is to subclass UITabBarController and selectively use the super implementation of weak var preferredFocusedView: UIView? { get } or one that returns selectedViewController?.preferredFocusView along with an implementation of didUpdateFocusInContext(_:withAnimationCoordinator:) that sets up an NSTimer that triggers a focus update and sets a flag that controls the preferredFocusView implementation.
More verbosely, Subclass UITabBarController and override didUpdateFocusInContext(context: UIFocusUpdateContext, withAnimationCoordinator coordinator: UIFocusAnimationCoordinator). In your implementation (make sure to call the super implementation) you can inspect the context and determine if a descendent view of the tabBar property is the nextFocusedView or the previousFocusedView (and the nextFocusedView is not a descendent).
If the tab bar is gaining focus you can create an NSTimer for the duration that you want to show the tab bar before hiding it. If the tab bar loses focus before the timer fires, invalidate it. If the timer fires, call setNeedsFocusUpdate() followed by updateFocusIfNeeded().
The last piece you need to get this to work is a flag that is set to true while the timer is set. You then need to override weak var preferredFocusedView: UIView? { get } and call the super implementation if the flag is false and if it is true return selectedViewController?.preferredFocusedView.
You can do it in a UITabBarController subclass:
final class TabBarViewController: UITabBarController {
private(set) var isTabBarHidden = false
func setTabBarHidden(_ isHidden: Bool, animated: Bool) {
guard isTabBarHidden != isHidden else {
return
}
var frame: CGRect
let alpha: CGFloat
if isHidden {
frame = tabBar.frame
frame.origin.y -= frame.height
alpha = 0
} else {
frame = tabBar.frame
frame.origin.y += frame.height
alpha = 1
}
let animations = {
self.tabBar.frame = frame
self.tabBar.alpha = alpha
}
if animated {
UIView.animate(withDuration: 0.3, animations: animations)
} else {
animations()
}
isTabBarHidden = isHidden
}
}
I have a very simple project, I want to animate the right margin on a label, however when I try it, it finishes instantly.
#IBAction func onDo(sender:UIButton)
{
self.view.setNeedsLayout()
self.testConstraint.constant = 40.0
UIView.animateWithDuration(2.0, animations: { () -> Void in
self.view.setNeedsLayout()
}) { (complete:Bool) -> Void in
}
}
The project is here:
https://www.dropbox.com/s/9a0v0906riunkww/test2222.zip?dl=0
Am I missing something obvious?
Update #1
It seems it's a problem with UILabels specifically, a standard UIView, or UIButton animates fine....so whats wrong with a UILabel?
Update #2
If I set the UILabel View Content Mode to Center, then it works fine, but it doesn't look very smooth...very strange.
I may be off the mark here, but it looks to me as though your animation routine just does setNeedslayout, which will cause the screen to redraw instantly. You need to change some animatable parameter of the UIView in order to see something move. Like self.view.frame, perhaps. The animatable properties of UIView are listed here.
Try replacing self.view.setNeedsLayout() in animation closure with self.view.layoutIfNeeded()
#IBAction func onDo(sender:UIButton) {
self.testConstraint.constant = 40.0
UIView.animateWithDuration(2.0, animations: { () -> Void in
self.view.layoutIfNeeded()
}) { (complete:Bool) -> Void in
}
}
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.