قالب وردپرس درنا توس
Home / IOS Development / ios – How to change the WIDTH of UITableView with animation?

ios – How to change the WIDTH of UITableView with animation?



I spent some time on your problem. Guess what?

It is not related to the table view at all. It is because of . Correct label alignment of the label.

You can recreate the error with a simple UILabel :

  class ViewController: UIViewController {

la label = Unable ()
var rightConstraint: NSLayoutConstraint!

override func loadView () {
view = UIView ()
view.backgroundColor = .white
}

override func viewDidLoad () {
super.viewDidLoad ()
setUpView ()
start()
}

func start () {
DispatchQueue.main.asyncAfter (deadline: .now () + 3) {
if self.rightConstraint.constant == 50 {
self.change (width: 100)
} other {
self.change (width: 50)
}
self.start ()
}
}

func change (width: CGFloat) {
rightConstraint.constant = width
UIView.animate (withDuration: 1
.0) { self.view.layoutIfNeeded () } } func setUpView () { label.text = "Label" label.textAlignment = .right view.addSubview (label) label.translatesAutoresizingMaskIntoConstraints = false rightConstraint = view.trailingAnchor.constraint (likeTo: label.trailingAnchor) rightConstraint.isActive = true label.leadingAnchor.constraint (likeTo: view.leadingAnchor) .isActive = true label.topAnchor.constraint (likeTo: view.topAnchor) .isActive = true label.bottomAnchor.constraint (likeTo: view.bottomAnchor) .isActive = true label.topAnchor.constraint (likeTo: view.topAnchor) .isActive = true } }

Why?

To find out the error, I created a UILABLE subclass and overpower action (for: CALayer, forKey: String) . This is where each UIView determines which animation should apply in response to a property change (ref). If you print the key you see actions being requested for the location, boundaries, and content of the label when UIKit is to commit the animations associated with ]

In fact, ] setNeedsDisplay called each time the size of a UILabel is changed (it makes sense). So I suppose there is a conflict between the content reduction of the label and the frame variation.

I think you should reproduce the correct text alignment of the label without using textAlignment = .right .

Horizontal, pin the label to the right only. When layoutIfNeeded will be called, only the label's position will change, not the content.


Source link