Slodin
Slodin

Reputation: 89

Swift UIButton changes size during localization but the bounds data doesn't get changed

I'm using UIBezierPath on a UIButton to give it rounded corners since IOS doesn't have native support for specific corners being rounded. I'm extending from UIView because I also needed this function in some modal views. For the most part, my solution worked because they were mostly icon buttons.

However, I noticed during localization, the actual size of the UIButton changes during runtime but the button's bounds/frame remains the same as the values in the storyboard. for example, in English, the button is width 70. In Chinese, it's 60 since the characters are shorter. This results in UIBezierPath is overdrawn (UI was first designed in English) to 70, when a user is using the Chinese localization (we don't have language switch, it's just based on IOS system language).

So in English, the button shows up correctly. But switching to other languages depending on character length, the button will remain at English's 70 widths thus will be shown as over draw or under draw to leave blank transparent gap. The print of bound/frame in viewDidLoad seems to remain the same as the storyboard, however, live capture of the UI reveals a different width.

UIButton doesn't have a preset width/height in the storyboard, it's greyed out so it depends on the text.

Extension.swift:

extension UIView {
    func cornerRadius(usingCorners corners: UIRectCorner, cornerRadius: CGSize) {
        let path = UIBezierPath(roundedRect: self.bounds, byRoundingCorners: corners, cornerRadii: cornerRadius)
        ...not important, no bounds/frame manupulation
    }
}

viewController.swift:

override func viewDidLoad() {
        super.viewDidLoad()
        takePhotoButton.cornerRadius(usingCorners: [.topRight, .bottomRight], cornerRadius: CGSize(size: 5))
    }

Upvotes: 0

Views: 195

Answers (1)

Shehata Gamal
Shehata Gamal

Reputation: 100533

Correct bounds is inside viewDidLayoutSubviews not viewDidLoad

override func viewDidLayoutSubviews() {
    super.viewDidLayoutSubviews()
    takePhotoButton.cornerRadius(usingCorners: [.topRight, .bottomRight], cornerRadius: CGSize(size: 5))
}

Upvotes: 1

Related Questions