问题
I am working on app which support English and Arabic languages.
My requirement is when I choose English, all the UIView
s should be displayed from Left to Right.
(ie. on the left side there is an UIImageView
and UILabel
on the Right of UIImageView
, see in pic below)
But when user choose Arabic language then UIImageView
should come to right side of screen and UILabel
should come to the Left of UIImageView
.
Is it possible to do in iOS or I should create separte ViewController
layout for Arabic language ????
Here i need to change my layout
回答1:
There are two ways of localisation,
Localisation- You can do it using single storyboard, you just have to set the leading space and trailing space constraint carefully and as you change the language the OS will take care of making the mirror image of English version of Views (especially for RTL languages). How this works is, when you change the language from device setting and then when you launch the app, the iOS checks for the selected language and then loads the storyboard for that specific language, but in your case you have to provide the language change feature in the app itself then you have to write some extra code to change the language on the fly.
Creating separate storyboard- Second one is creating separate storyboards for each language and design the screens for English language first then just copy and paste the same in Arabic language storyboard and the just shift the component to make mirror image of the same.
Note:
In both ways you are providing the user to change the application language in the app itself so for that you have to take some extra efforts as the default behaviour of localisation thing is you need to restart the app to see the effect and the application language will change when you change the device language, you can not simply change the language on the fly and see the UI change taking place, this will not happen.
So what you have to do in terms of coding part to change the language in the app and user should be able to see the UI in the selected language is explained below,
Which is the better way?
If the app has simple UI and also has small scope then you can go for localisation that is single storyboard for multiple languages.
But if the app has complex UI and the scope of the app is also fairly big then i would suggest you to go for separate storyboard as using localisation has some limitations or we can say that it is tricky to set constraints for complex UI, if you got stuck in the UI part then it could take more time, this would not be the case in using separate storyboard as you can make the changes easily as you want.
Now lets see some code,
Below is the code for switching between two storyboards,
AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
if ([kGetSelectedLanguage isEqualToString:kEnglish]) { //Load Arabic Storyboard
[sender setSelected:NO];
// Get English Language Storyboard
UIStoryboard *storyBoard = [UIStoryboard storyboardWithName:@"Arabic" bundle:nil];
// Create Navigation controller with RootViewController
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:[storyBoard instantiateViewControllerWithIdentifier:@"YourRootViewController"]];
// Set Windows RootViewController
[appDelegate.window setRootViewController: navigationController];
[[NSUserDefaults standardUserDefaults] setObject:kArabic forKey:kSelectedLanguage];
[[NSUserDefaults standardUserDefaults] synchronize];
} else { //Load English Storyboard
[sender setSelected:YES];
// Get English Language Storyboard
UIStoryboard *storyBoard = [UIStoryboard storyboardWithName:@"English" bundle:nil];
// Create Navigation controller with RootViewController
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:[storyBoard instantiateViewControllerWithIdentifier:@"YourRootViewController"]];
// Set Windows RootViewController
[appDelegate.window setRootViewController: navigationController];
[[NSUserDefaults standardUserDefaults] setObject:kEnglish forKey:kSelectedLanguage];
[[NSUserDefaults standardUserDefaults] synchronize];
}
And this is the code to re instantiate the storyboard in case of localisation (and changing the language in the app itself),
//Change language to English
AppDelegate *delegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
[delegate setCountryLanguageCode:@"US" AndLanguageCode:@"en"];
//Change language to Arabic
AppDelegate *delegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
[delegate setCountryLanguageCode:@"EG" AndLanguageCode:@"ar"];
//Re-instantiate the storyboard
-(void)setCountryLanguageCode:(NSString *)countryCode AndLanguageCode:(NSString *)languageCode{
[NSBundle setLanguage:languageCode];
UIStoryboard *storyRef = [UIStoryboard storyboardWithName:@"Main" bundle:[NSBundle mainBundle]];
self.window.rootViewController = [storyRef instantiateInitialViewController];
}
Note:
Providing the in app language change feature is not recommended by Apple as is could lead to confusion between the device selected language and the application language but if it is your requirement and you have to do this anyway then the choice is yours.
Hope it helps you.
回答2:
It is already done for you. Use Autolayout to create you UI (NSLayoutConstraint
). When creating horizontal view hierarchy use NSLayoutAttributeLeading
and NSLayoutAttributeTrailing
attributes. First will correspond to the left edge in English device locale and right edge in Arabic. And vice versa
Check this tutorial for more info https://developer.apple.com/library/ios/documentation/MacOSX/Conceptual/BPInternational/InternationalizingYourUserInterface/InternationalizingYourUserInterface.html
回答3:
the best way to do this is to manage it with NSLayoutConstraint. Please refer to psuedo code below:
- (void)createConstraints {
/* Constraint creation methods. See comments in each method for more detail. */
[self createCompactConstraints];
[self createRegularConstraints];
/* Activate a set of constraints for initial layout. */
[NSLayoutConstraint activateConstraints: rightSideConstraint];
[NSLayoutConstraint activateConstraints: leftSideConstraint];}
- (void)createLeftConstraints {
/*
Use NSLayoutConstriant class to implement the layout for the required label.
*/}
- (void)createRightConstraints {
/*
Use NSLayoutConstriant class to implement the layout for the required label.
*/}
- (void)changeLayout{
if(!Arabic){
/*
Deactivate left to right label constraint
Activate right to left label constraint
*/
}
else{
/*
Deactivate right to left label constraint
Activate left to right label constraint
*/
} }
来源:https://stackoverflow.com/questions/38631740/how-to-shift-all-uiview-from-left-to-right-in-ios-objective-c