私の iOS5 iPhone アプリケーションでは、次のコードを使用して検索バーの色合いを設定しています。
searchBar.tintColor = UIColorMake(@"#EFEFEF");
#efefef の RGB 値は (239,239,239) で、正常に
動作しています。ただし、キャンセル ボタンが表示されると、「キャンセル」というテキストは表示されません。透明な白黒テキストでキャンセル ボタンだけをカスタマイズすることはできますか?
カスタマイズは可能ですか?
私の iOS5 iPhone アプリケーションでは、次のコードを使用して検索バーの色合いを設定しています。
searchBar.tintColor = UIColorMake(@"#EFEFEF");
#efefef の RGB 値は (239,239,239) で、正常に
動作しています。ただし、キャンセル ボタンが表示されると、「キャンセル」というテキストは表示されません。透明な白黒テキストでキャンセル ボタンだけをカスタマイズすることはできますか?
カスタマイズは可能ですか?
外観プロキシを使用して、iOS 5 の [キャンセル] ボタンをカスタマイズできます。UIBarButtonItemに含まれている場合は、の外観を変更する必要がありますUISearchBar。たとえば、[キャンセル] ボタンのタイトル フォントを変更するには、次のコマンドを使用できます。
NSDictionary *attributes =
[NSDictionary dictionaryWithObjectsAndKeys:
[UIColor whiteColor], UITextAttributeTextColor,
[UIColor colorWithRed:0 green:0 blue:0 alpha:0.5], UITextAttributeTextShadowColor,
[NSValue valueWithUIOffset:UIOffsetMake(0, 1)], UITextAttributeTextShadowOffset,
[UIFont systemFontOfSize:12], UITextAttributeFont,
nil];
[[UIBarButtonItem appearanceWhenContainedIn:[UISearchBar class], nil]
setTitleTextAttributes:attributes forState:UIControlStateNormal];
[[UIBarButtonItem appearanceWhenContainedIn:[UISearchBar class], nil]
setTitleTextAttributes:attributes forState:UIControlStateHighlighted];
サブビューを検索してキャンセル ボタンを見つけることができUISearchBarます。ボタンが変更される可能性があるため、これを行うのは危険です。たとえば、これをviewWillAppear
- (void) viewWillAppear:(BOOL)animated
{
//show the cancel button in your search bar
searchBar.showsCancelButton = YES;
//Iterate the searchbar sub views
for (UIView *subView in searchBar.subviews) {
//Find the button
if([subView isKindOfClass:[UIButton class]])
{
//Change its properties
UIButton *cancelButton = (UIButton *)[sb.subviews lastObject];
cancelButton.titleLabel.text = @"Changed";
}
}
}
これが変わる可能性がある前に言ったように、そうするのはハックです。オリジナルに固執するか、独自の検索バーを作成することをお勧めします.
iOS5以降、このコードでナビゲーションバー、ツールバー、タブバーなどを編集できます...
NSDictionary *textTitleOptions = [NSDictionary dictionaryWithObjectsAndKeys:
[UIColor darkGrayColor],
UITextAttributeTextColor,
[UIColor whiteColor],
UITextAttributeTextShadowColor, nil];
[[UINavigationBar appearance] setTitleTextAttributes:textTitleOptions];
検索バーでテストしていませんが、同様に機能するはずです。
この方法はIOS7で機能します
for (UIView *view in searchBar.subviews)
{
for (id subview in view.subviews)
{
if ( [subview isKindOfClass:[UIButton class]] )
{
// customize cancel button
UIButton* cancelBtn = (UIButton*)subview;
[cancelBtn setEnabled:YES];
break;
}
}
}