Abedalkareem / LanguageManager-iOS

A Language manager to handle changing app language without restarting the app. It supports iOS and tvOS.
MIT License
396 stars 67 forks source link

Custom View not showing in storyboard #71

Open MMohamad70 opened 2 years ago

MMohamad70 commented 2 years ago

Hi, I am using this library and managed to call localiz() after awakeFromNib() method is called. Everything works fine on the simulator. but when I create custom views (@IBDesignable component) and add them to the ViewController on the storyboard, they cannot be rendered and show errors. I debug the component and it crash on localiz() line and say that default language not set, although I've added the default language on AppDelegate. Can you suggest what is the problem and how can I solve this?

ZainabAl-khabori commented 1 year ago

The @IBDesignable agent most likely uses a different delegate to initialize, not the AppDelegate. The default language isn't set there. I suggest that you set the default language once again in your prepareForInterfaceBuilder(). It would not have an effect on the application while it's running, since it is only called from the Interface Builder and not the system, according to Apple's documentation:

When Interface Builder instantiates a class with the IB_DESIGNABLE attribute, it calls this method to let the resulting object know that it was created at design time. You can implement this method in your designable classes and use it to configure their design-time appearance. For example, you might use the method to configure a custom text control with a default string. The system does not call this method; only Interface Builder calls it.

Interface Builder waits until all objects in a graph have been created and initialized before calling this method. So if your object’s runtime configuration relies on subviews or parent views, those objects should exist by the time this method is called.

Your implementation of this method must call super at some point so that parent classes can perform their own custom setup.

You can setup the language anywhere in the method, but I opted to set it before calling super, in order to avoid any complications that might arise there.

override func prepareForInterfaceBuilder() { LanguageManager.shared.defaultLanguage = Languages.en // The lang you want your view to appear in super.prepareForInterfaceBuilder() initView() }

Abedalkareem commented 11 months ago

image

The @IBDesignable will be removed in the next releases as it's deprecated