2016-05-02 4 views
-6

次の目的コードをAppDelegate.swiftファイルに追加する必要があります。目的のCコードをスウィフトファイルに追加する

// Define some colors. 
    UIColor *darkGray = [UIColor darkGrayColor]; 
    UIColor *lightGray = [UIColor lightGrayColor]; 

    // Navigation bar background. 
    [[UINavigationBar appearance] setBarTintColor:darkGray]; 
    [[UINavigationBar appearance] setTintColor:lightGray]; 

    // Color of typed text in the search bar. 
    NSDictionary *searchBarTextAttributes = @{ 
     NSForegroundColorAttributeName: lightGray, 
     NSFontAttributeName : [UIFont systemFontOfSize:[UIFont systemFontSize]] 
    }; 
    [UITextField appearanceWhenContainedInInstancesOfClasses:@[[UISearchBar class]]] 
    .defaultTextAttributes = searchBarTextAttributes; 

    // Color of the placeholder text in the search bar prior to text entry. 
    NSDictionary *placeholderAttributes = @{ 
     NSForegroundColorAttributeName: lightGray, 
     NSFontAttributeName : [UIFont systemFontOfSize:[UIFont systemFontSize]] 
    }; 

    // Color of the default search text. 
    // NOTE: In a production scenario, "Search" would be a localized string. 
    NSAttributedString *attributedPlaceholder = 
     [[NSAttributedString alloc] initWithString:@"Search" 
    attributes:placeholderAttributes]; 
    [UITextField appearanceWhenContainedInInstancesOfClasses:@[[UISearchBar class]]] 
    .attributedPlaceholder = attributedPlaceholder; 

最適なソリューションは何ですか? Objective-Cのファイルやヘッダーを作成するだけですか?

+2

すぐに翻訳してAppDelegate.swiftに直接書き込んでください。 – keithbhunter

答えて

0

これはスウィフトへの大まかな翻訳です...テストされていません。

// Define some colors. 
var darkGray: UIColor = UIColor.darkGrayColor() 
var lightGray: UIColor = UIColor.lightGrayColor() 
// Navigation bar background. 
UINavigationBar.appearance().barTintColor = darkGray 
UINavigationBar.appearance().tintColor = lightGray 
    // Color of typed text in the search bar. 
var searchBarTextAttributes: [NSObject : AnyObject] = [NSForegroundColorAttributeName: lightGray, NSFontAttributeName: UIFont.systemFontOfSize(UIFont.systemFontSize())] 
UITextField.appearanceWhenContainedInInstancesOfClasses([UISearchBar.self]).defaultTextAttributes = searchBarTextAttributes 
    // Color of the placeholder text in the search bar prior to text entry. 
var placeholderAttributes: [NSObject : AnyObject] = [NSForegroundColorAttributeName: lightGray, NSFontAttributeName: UIFont.systemFontOfSize(UIFont.systemFontSize())] 

    // Color of the default search text. 
    // NOTE: In a production scenario, "Search" would be a localized string. 
var attributedPlaceholder: NSAttributedString = NSAttributedString(string: "Search", attributes: placeholderAttributes) 
UITextField.appearanceWhenContainedInInstancesOfClasses([UISearchBar.self]).attributedPlaceholder = attributedPlaceholder 
+0

はい、これは正しいです。私はObjective-CからSwiftへの変換があまり良くありません。 – Slava

+2

ほとんどの場合、変数の明示的な型を定義する必要はありません。Swiftに推論させるだけです。また、変更しない変数に対しては 'let'を使用するべきです。 – Hamish

関連する問題