如何在iOS中将所有UIView从左向右移动? (目标C) [英] How to shift all UIView From Left to Right in iOS? (Objective-C)

查看:511
本文介绍了如何在iOS中将所有UIView从左向右移动? (目标C)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发支持英语和阿拉伯语的应用程序。



我的要求是当我选择英语时,所有 UIView 应该从左到右显示。 >
(即左侧右侧有 UIImageView UILabel UIImageView ,见下图)



但是当用户选择阿拉伯语时,则 UIImageView 应该出现在屏幕右侧, UILabel 应该出现在 UIImageView



是否可以在iOS中执行或者我应该为阿拉伯语创建separte ViewController 布局? ?????? b

我需要更改布局



解决方案

有两种本地化方式,




  • 本地化 - 您可以使用单个故事板来完成它,您只需要仔细设置前导空格和尾随空间约束,当您更改语言时,操作系统将负责制作英文版Views的镜像(特别是RTL语言)。这是如何工作的,当您从设备设置更改语言然后启动应用程序时,iOS会检查所选语言,然后加载该特定语言的故事板,但在您的情况下,您必须提供语言更改功能在应用程序本身,你必须编写一些额外的代码来动态更改语言。


  • 创建单独的故事板 - 第二一个是为每种语言创建单独的故事板,并首先设计英语语言屏幕,然后只需将其复制并粘贴到阿拉伯语故事板中,只需移动组件即可制作相同的镜像。




注意:



以这两种方式为用户提供服务更改应用程序本身的应用程序语言,因此您必须采取一些额外的努力,因为本地化的默认行为是您需要重新启动应用程序以查看效果和应用程序更改设备语言时语言会发生变化,你不能简单地改变语言并看到UI变化发生,这种情况不会发生。



那么什么你需要做的是编写部分来改变应用程序中的语言,用户应该能够看到所选语言的用户界面如下所述,



哪种方式更好?



如果应用程序具有简单的用户界面且范围较小,那么您可以进行多语言的单一故事板本地化。



但如果应用程序具有复杂的用户界面且应用程序的范围也相当大,那么我建议你去单独的故事板,因为使用本地化有一些限制或我们可以说为复杂的UI设置约束是很棘手的,如果你陷入UI部分然后它可能需要更多的时间,这不是使用单独的故事板的情况,因为你可以根据需要轻松地进行更改。



现在让我们看一些代码,



以下是两个故事板之间切换的代码,

  AppDelegate * appDelegate =(AppDelegate *)[[UIApplication sharedApplication] delegate]; 

if([kGetSelectedLanguage isEqualToString:kEnglish]){//加载阿拉伯语故事板

[发送者setSelected:NO];

//获取英语故事板
UIStoryboard * storyBoard = [UIStoryboard storyboardWithName:@Arabicbundle:nil];

//使用RootViewController创建导航控制器
UINavigationController * navigationController = [[UINavigationController alloc] initWithRootViewController:[storyBoard instantiateViewControllerWithIdentifier:@YourRootViewController]];

//设置Windows RootViewController
[appDelegate.window setRootViewController:navigationController];

[[NSUserDefaults standardUserDefaults] setObject:kArabic forKey:kSelectedLanguage];

[[NSUserDefaults standardUserDefaults] synchronize];

} else {//加载英语故事板

[发送者setSelected:YES];

//获取英语语言故事板
UIStoryboard * storyBoard = [UIStoryboard storyboardWithName:@Englishbundle:nil];

//使用RootViewController创建导航控制器
UINavigationController * navigationController = [[UINavigationController alloc] initWithRootViewController:[storyBoard instantiateViewControllerWithIdentifier:@YourRootViewController]];

//设置Windows RootViewController
[appDelegate.window setRootViewController:navigationController];

[[NSUserDefaults standardUserDefaults] setObject:kEnglish forKey:kSelectedLanguage];

[[NSUserDefaults standardUserDefaults] synchronize];
}

这是在本地化的情况下重新实例化故事板的代码(和更改应用程序本身的语言),

  //将语言更改为英语
AppDelegate * delegate =(AppDelegate * )[UIApplication sharedApplication] .delegate;
[delegate setCountryLanguageCode:@USAndLanguageCode:@en];

//将语言更改为阿拉伯语
AppDelegate * delegate =(AppDelegate *)[UIApplication sharedApplication] .delegate;
[delegate setCountryLanguageCode:@EGAndLanguageCode:@ar];

//重新实例化storyboard
- (void)setCountryLanguageCode:(NSString *)countryCode AndLanguageCode:(NSString *)languageCode {
[NSBundle setLanguage:languageCode];
UIStoryboard * storyRef = [UIStoryboard storyboardWithName:@Mainbundle:[NSBundle mainBundle]];
self.window.rootViewController = [storyRef instantiateInitialViewController];
}

注意:



Apple不建议提供应用程序内语言更改功能,因为它可能导致设备选择语言和应用程序语言之间的混淆,但如果这是您的要求,那么无论如何您必须这样做选择是你的。



希望它可以帮到你。


I am working on app which support English and Arabic languages.

My requirement is when I choose English, all the UIViews 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

解决方案

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.

这篇关于如何在iOS中将所有UIView从左向右移动? (目标C)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆