2016-07-28 65 views
0

我正在開發支持英語和阿拉伯語的應用程序。如何在iOS中將所有UIView從左移到右? (Objective-C)

我的要求是,當我選擇英語,所有的UIView S的關係顯示由左到右。
(即左側上有UIImageView一個UIImageViewUILabel,請參見下面的圖)

但是,當用戶選擇阿拉伯語然後UIImageView應該來屏幕和UILabel左右兩邊來到UIImageView的左邊。

是否有可能在iOS的做,或者我應該創建separte ViewController佈局阿拉伯語????

在這裏,我需要改變我的佈局

enter image description here

+0

你是否在應用程序本身提供語言更改選項,如從設置屏幕或其他?你有沒有使用本地化 –

+1

是的,我已經在設置頁面 – Dhiru

+0

@Dhiru提供了該方法,這實際上不是一個好主意。不好的做法,我建議你堅持使用國際化工具Apple已經爲開發者提供了 - https://developer.apple.com/library/ios/documentation/MacOSX/Conceptual/BPInternational/Introduction/Introduction.html –

回答

1

有兩種方式定位,

  • 本地化 - 您可以使用單一的故事板做的,你只需要仔細設置前導空間和尾隨空間約束,並且在更改語言時,操作系統將負責製作視圖英文版的鏡像(尤其是對於RTL語言)。這是如何工作的,當您從設備設置中更改語言,然後啓動應用程序時,iOS會檢查所選語言,然後加載該特定語言的故事板,但在您的情況下,您必須提供語言更改功能在應用程序本身,那麼你必須編寫一些額外的代碼來動態改變語言。

  • 創建單獨的故事板 - 第二個是創建爲每種語言獨立的情節串連圖板和設計英語第一,然後只複製屏幕並粘貼同在阿拉伯語故事板和剛轉移到使鏡像分量一樣。

注:

在這兩種方法,你所提供的用戶更改應用程序語言的應用程序本身所以,你必須採取一些額外的努力,國產化的東西的默認行爲是您需要重新啓動應用程序才能看到效果,並且在更改設備語言時應用程序語言會發生變化,您不能簡單地更改動態語言並查看發生的UI更改,這種情況不會發生。

所以,你必須在編碼部分更改應用程序和用戶的語言應該能夠看到所選語言的用戶界面如下解釋方面做什麼,

哪個是更好的辦法?

如果應用程序具有簡單的用戶界面,也有小範圍,那麼你可以去本地化,這是多個語言的單一故事板。

但是,如果應用程序有複雜的用戶界面,應用程序的範圍也相當大,那麼我建議你去單獨的故事板,因爲使用本地化有一些限制,或者我們可以說,設置複雜的約束是棘手的用戶界面,如果你被困在用戶界面部分,那麼可能需要更多的時間,在使用單獨的故事板時,情況並非如此,因爲您可以根據需要輕鬆進行更改。

現在讓我們看看一些代碼,

下面是兩個故事板之間切換的代碼,

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]; 
} 

這是重新實例化故事板中的定位的情況下(代碼並更改應用程序本身的語言),

//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]; 
} 

注:

提供了在應用程序不推薦蘋果語言切換功能的是可能導致設備選擇的語言和應用語言之間的混淆,但如果它是你的要求,你必須做這個反正那麼選擇是你的。

希望它可以幫助你。

+1

非常感謝你的回答!!!! – Dhiru

+2

您可以投票並接受解決問題的答案,這將有助於其他面臨同樣問題的人。 –

0

它已經爲你做了。使用Autolayout創建UI(NSLayoutConstraint)。創建水平視圖層次結構時,請使用NSLayoutAttributeLeadingNSLayoutAttributeTrailing屬性。首先將對應英文設備區域設置的左邊緣和阿拉伯語的右邊緣。反之亦然

檢查本教程以獲得更多信息https://developer.apple.com/library/ios/documentation/MacOSX/Conceptual/BPInternational/InternationalizingYourUserInterface/InternationalizingYourUserInterface.html

0

這樣做的最好方法是用NSLayoutConstraint進行管理。請參閱下面的僞代碼:

- (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 
    */ 
} } 
+1

感謝您的回答,但您能否告訴我如何停用從左到右的標籤約束並激活從右到左的標籤約束?編程? – Dhiru

+1

@Dhiru,您可以使用以下NSLayoutConstriant方法以編程方式禁用約束。 [NSLayoutConstraint deactivateConstraints:NSArray ]; –