PinLayout icon indicating copy to clipboard operation
PinLayout copied to clipboard

Why cannot resizing UIView?

Open Skyline-23 opened this issue 3 years ago • 1 comments

I am trying to resize with animation but it not works. (only animation works)

this is my screen recording Simulator Screen Recording - iPhone 13 Pro - 2021-12-29 at 19 44 04

and this is my code

import UIKit
import RxSwift
import RxGesture
import Then
import PinLayout

class ViewController: UIViewController {

    private let view1: UIView = UIView().then {
        $0.backgroundColor = .lightGray
    }
    
    let disposeBag = DisposeBag()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        [view1].forEach { self.view.addSubview($0) }
        bind()
    }
    
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        super.touchesBegan(touches, with: event)
        
        self.view.endEditing(true)
    }
    
    // SafeArea, Rotate Code need to be written here
    override func viewDidLayoutSubviews() {
        self.view1.pin
            .center()
            .size(30)
    }
    
    private func bind() {
        
        self.view1.rx.tapGesture()
            .subscribe(onNext: { [weak self] _ in
                guard let self = self else { return }
                
                UIView.animate(withDuration: 0.2) {
                    self.view1.pin
                        .size(100)
                }

            }).disposed(by: disposeBag)
    }

}

Skyline-23 avatar Dec 29 '21 10:12 Skyline-23

There could be a conflict between viewDidLayoutSubviews() (which set the size to 30), and the animation block who set the size to 100. You should update a property containing the size and call self.view.setNeedsLayout() from your animation block. But the exact reason why it doesn't work is strange. It would probably won't work either setting direction the frame property? (self.view.frame = CGRect(0,0,100,100))

lucdion avatar Dec 29 '21 15:12 lucdion