1

I am having an application in both LTR and RTL languages. Everything is fine but the UIButton title alignment is not changing to right though the UIButton flips itself. Here is the screenshot: UIButton in RTL

Alignment Given

Constraints given for default language english:

Constraints given for default language english:

What is the issue and why it happens? Do we need to set the title programatically aligned to right?

Chelsea Shawra
  • 1,593
  • 4
  • 22
  • 43

3 Answers3

1

Use right alignment from storyboard as

enter image description here

or from programmatically as

buttonShowOnMap.contentHorizontalAlignment = .left//For left alignment

buttonShowOnMap.contentHorizontalAlignment = .right//For right alignment
Jack
  • 13,571
  • 6
  • 76
  • 98
  • 1
    what for left alligment . My application supports both RTL and LTR so setting in storyboard will no work – Chelsea Shawra Jul 26 '17 at 06:13
  • This answer supports both, rtl and ltr: https://stackoverflow.com/questions/36464708/trouble-left-aligning-uibutton-title-ios-swift/36464814 – iljer Nov 07 '18 at 10:50
  • 1
    in newer XCode.InterfaceBuilder they have included a leading and trailing Alignment option, that can be useful – Abduliam Rehmanius Mar 18 '20 at 13:06
1

Accepted answer did not help me. But following snippet worked to support both RTL and LTR languages automatically:

// makes button content support RTL <-> LTR
button.contentHorizontalAlignment = .leading

// makes titleLabel support RTL <-> LTR
button.titleLabel?.textAlignment = .natural 
Artem
  • 331
  • 1
  • 13
0

No idea why happening but you must subclass UIButton so that you can override alignmentRectInsets.

override func alignmentRectInsets() -> UIEdgeInsets {
    var insets: UIEdgeInsets
    if (Left_Side_Button) {
        insets = UIEdgeInsetsMake(0, 7.0, 0, 0)
    }
    else {
        // Right Button
        insets = UIEdgeInsetsMake(0, 0, 0, 7.0)
    }
    return insets
}

And achieve it by above code.

Govaadiyo
  • 5,644
  • 9
  • 43
  • 72