-1

I am using MDC (Material design components) to build my UI in an iOS application, I have 2 languages in my app english and arabic, when switching to arabic I am forcing right to left to all my views, it works great as shown in the pictures below:

enter image description here

forcing RTL on all view in AppDelegate as follow:

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        UIView.appearance().semanticContentAttribute = .forceRightToLeft
    }

the result is:

enter image description here

But the problem when i push a view controller in left to right mode it looks like this:

enter image description here

But in RTL Mode it look as following:

enter image description here

I am trying to flip the back button horizontally but with no success here is the code in my pushed view controller:

class UserCartViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
    }

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        (UIApplication.shared.delegate as! AppDelegate).appBar?.navigationBar.backItem?.image?.mdf_imageWithHorizontallyFlippedOrientation()
    }

}

the backItem is nil in this case but when I delay the code:

class UserCartViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
    }

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        DispatchQueue.main.asyncAfter(deadline: .now() + 2.0) {
            (UIApplication.shared.delegate as! AppDelegate).appBar?.navigationBar.backItem?.image?.mdf_imageWithHorizontallyFlippedOrientation()
        }
    }

}

the backItem has a value and an image but mdf_imageWithHorizontallyFlippedOrientation() is not working so my questions:

  • Is there a better way to fix the default back button of MDCAppBar to be flipped when i force all views to be RTL?
  • Why after time delay I get values for the back button is there any function or delegate triggered when the MDCAppBar is ready?
  • And finally why mdf_imageWithHorizontallyFlippedOrientation() is not working on an image?

Thanks in advance

Antoine El Murr
  • 317
  • 1
  • 13

1 Answers1

1

In RTL only object change place right to left but Image displayed as it is(image will not flip), So, In this case, you need to check condition if current app language is RTL then use image accordingly, See following code.

if(UIApplication.shared.userInterfaceLayoutDirection == .rightToLeft) {
    //RTL //assign forword arraow
} else {
    //LTR //Assign back arrow
}

see this post for more info https://medium.com/if-let-swift-programming/working-with-localization-in-swift-4a87f0d393a4

AtulParmar
  • 4,358
  • 1
  • 24
  • 45
  • thanks @AtulParmar for your answer, but I didn't assign any back button image this is the default image for MDCAppBar https://material.io/develop/ios/components/app-bars/, and this button is created on push viewcontroller but as my question listed when viewWillAppear triggered my backItem is nil only after a certain of time the button is not nil and I need to avoid to delay my code even to do this workaround – Antoine El Murr Feb 28 '19 at 09:14
  • can you assign a custom image for this, If yes then you need to change according to language – AtulParmar Feb 28 '19 at 09:16
  • I can but the problem is when the view appear the button is not created directly, because as I listed the button is nil on viewAppear, after I delay the code the button is not nil, is there a way to check if the MDCAppBar has fully been created ? – Antoine El Murr Feb 28 '19 at 09:19
  • Sorry I have no idea about MDCAppBar but if there is no any option then use native notification bar and set image for for this. – AtulParmar Feb 28 '19 at 09:23
  • Yeah I need to use material design for this app, it's the client request so I'm stuck with the MDCAppBar, the native app bar flip the back button all by it self :'(, anw thank you for your answer – Antoine El Murr Feb 28 '19 at 09:25