Search code examples
iosswiftios13

why my icon has a different behaviour in ios13


It's been a long time I haven't been working on an ios project. Anyway, I have an icon that is behaving differently on ios13.

when I change the values, it seems to have an effect on devices running ios 12 and lower but not 13 see image

let showImage : UIImage = UIImage(named: "show-512")!
showHideButton.setImage(showImage, for: UIControlState())
showHideButton.frame = CGRect(x: -10.0, y: 0.0, width: 30.1, height: 22.2)

Solution

  • This is an undefined behavior of iOS 13 about textField's left/right view.

    You should manually add width constraint to the rightView/leftView.

    Don't forget to set translatesAutoresizingMaskIntoConstraints = false

    showHideButton.translatesAutoresizingMaskIntoConstraints = false
    showHideButton.widthAnchor.constraint(equalToConstant: <#NeededWidth#>).isActive = true
    // This is enough to make it act like before but you can set other missing constraints like height to suppress layout warnings and prevent further issues.
    // showHideButton.widthAnchor.constraint(equalToConstant: <#HeightOfTheTextField#>).isActive = true
    

    You may notice some autolayout warnings in the consule because you didn't set the missing constraint for the rightView/leftView. So add missing constraints or simply ignore those.

    And note that if the rightView/leftView is some kind of StackView, try to putting it inside a view and then add this view instead.

    - More Information

    From the iOS & iPadOS 13 Developer Beta 5 Release Notes:

    UIKit - Resolved Issues

    Prior to iOS 13, UITextField assumed that the frames of its leftView and rightView were correctly set when assigned and would never change. Starting in iOS 13, the implementation of leftViewRect(forBounds:) and rightViewRect(forBounds:) now ask the view for its systemLayoutSizeFitting(:). To achieve the previous behavior when linking against and running on iOS 13, add explicit sizing constraints on the view, wrap it in a plain UIView, or subclass the view and implement systemLayoutSizeFitting(:). (51787798)