roberthein / TinyConstraints

Nothing but sugar.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Implementations of `trailingToSuperview` and `leadingToSuperview` only work correct only for Left-to-Right languages / layout.

therohansanap opened this issue · comments

What layout do I expect?

I want my signupButton to have it's trailing edge 20 points before safeAreaLayoutGuide.centerXAnchor. Similarly, I want my loginButton to have its leading edge 20 points after safeAreaLayoutGuide.centerXAnchor. Below image displays the layout in desired state:
LTR
Left-to-Right Layout 👆🏾
RTL-correct
Right-to-Left Layout 👆🏾

What does my code looks like?

let signupButton = UIButton(type: .system)
signupButton.setTitle("Signup", for: .normal)
view.addSubview(signupButton)
signupButton.bottomToSuperview(offset: -50)
signupButton.trailingToSuperview(view.safeAreaLayoutGuide.centerXAnchor, offset: 20)

let loginButton = UIButton(type: .system)
loginButton.setTitle("Login", for: .normal)
view.addSubview(loginButton)
loginButton.bottomToSuperview(offset: -50)
loginButton.leadingToSuperview(view.safeAreaLayoutGuide.centerXAnchor, offset: 20)

What output do I get with above code?

Below are the screenshots of my output:
LTR
Left-to-Right Layout 👆🏾
RTL-bug
Right-to-Left Layout 👆🏾

The problem

It looks like the way superview methods are implemented, they work correct only for Left-to-Right language / layout direction.

Well, I have been using ternary operator in order to pass proper values for RTL (myRandomImageView.leadingToSuperview(offset: isRTL ? -11 : 11)). But now I realized it's not the best approach as it can be fixed in the future. So the layout would get "broken" for RTL languages again. So better is to go with leading(to:) / trailing(to:) methods.

Hi @petrkahanek

I created a pull request for this issue

#108