[英]How to change button color on UIImagePickerController navigation bar?
我設法改變了navigation bar
的顏色,但按鈕的顏色仍然是黑色。 有沒有辦法改變按鈕的顏色? 見下圖。
更新 :抱歉由於版權問題刪除了圖像。
沒有您的自定義圖像是不可能的。
但是您應該在創建導航控制器后立即更改色調顏色:
UIImagePickerController *pickerImg = [[UIImagePickerController alloc] init];
pickerImg.navigationBar.barStyle = UIBarStyleBlackOpaque; // Or whatever style.
// or
pickerImg.navigationBar.tintColor = [UIColor whateverColor];
您可以隱藏導航欄,並使用imageview創建自定義導航欄並在其上放置2個按鈕。通過這樣做,您可以根據您的要求更改按鈕的圖像/顏色以及圖像視圖。
隱藏導航欄:
self.navigationController.navigationBarHidden=YES;
然后在XIB中:創建一個44高度的視圖,在其上放置imageview並在其上放置2個按鈕和1個標簽。
希望這能解決你的問題。
@Ankit您可以嘗試從導航欄中獲取子視圖,然后設置所需的顏色。 我使用這種方法在搜索欄上設置取消按鈕的顏色。 取消按鈕的大小為48 * 30,您可以使用子視圖數組來設置所需的顏色。 我不確定,但這可能會起作用。 下面是我用來設置搜索欄上取消按鈕顏色的代碼。
NSArray *arrySearchSubviews=[searchBarFrnds subviews];
for (UIButton *btn in arrySearchSubviews)
{
CGRect rect=btn.frame;
NSLog(@"width %f",rect.size.width);
NSLog(@"height %f",rect.size.height);
if (rect.size.width==48 )
{
[btn setTintColor:[UIColor colorWithRed:0 green:0.403f blue:0.4f alpha:1.0f]];
}
}
如果您想影響所有視圖中出現的按鈕的顏色,例如您希望更改的按鈕的顏色,則只需使用以下內容:
UIBarButtonItem *searchBarButton = [UIBarButtonItem appearanceWhenContainedIn:[UISearchBar class], nil];
[searchBarButton setTintColor:[UIColor colorWithRed:138.0/255.0 green:183.0/255.0 blue:64.0/255.0 alpha:1.0]];
我希望它會對你有所幫助!
步驟1:
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
[self presentViewController:picker animated:YES completion:NULL];
第2步:
/* Status bar color */
[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault];
/* Left button */
navigationController.navigationBar.tintColor = [UIColor blackColor];
/* Right button color */
navigationController.navigationBar.topItem.rightBarButtonItem.tintColor = [UIColor blackColor];
- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
if ([navigationController isKindOfClass:[UIImagePickerController class]]) {
if ([[UIDevice currentDevice] systemVersion].floatValue >= 8.0) {
[viewController.navigationController.navigationBar setBarTintColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"navbar1"]]];
} else {
[viewController.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"navbar2"] forBarMetrics:UIBarMetricsDefault];
}
viewController.navigationController.navigationBar.tintColor = [UIColor whiteColor];
UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 40)];
titleLabel.textColor = [UIColor whiteColor];
titleLabel.textAlignment = NSTextAlignmentCenter;
titleLabel.font = [UIFont boldSystemFontOfSize:18.0f];
titleLabel.text = @"photos";
[viewController.navigationItem setTitleView:titleLabel];
viewController.edgesForExtendedLayout = UIRectEdgeNone;
}
}
從iOS 5及更高版本開始,您可以使用新的UIAppearance
API實現所需的自定義。 本教程基本上向您展示了如何。
您必須為BarButtonItem定義UIButtonTypeCustom按鈕。
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
UIBarButtonItem *yourBarButton = [[UIBarButtonItem alloc] initWithCustomView:button];
有關更多信息,請查看此帖: 帶顏色的UIBarButtonItem?
如果您不介意在整個應用程序中使用相同的樣式,請使用@Bourne建議的外觀:
let font = UIFont(name:"Montserrat-Bold", size:16)!
let attributes: LTDictStrObj = [
NSForegroundColorAttributeName: UIColor.whiteColor(),
NSFontAttributeName: font,
]
UIBarButtonItem.appearance().setTitleTextAttributes(attributes, forState: UIControlState.Normal)
如果你像我一樣使用白色,你也可以改變導航欄的背景顏色:
picker.navigationBar.barTintColor = .redColor()
使用UIAppearance的一個班輪:
[[UINavigationBar appearanceWhenContainedInInstancesOfClasses:@[[UIImagePickerController class]]] setTintColor:[UIColor redColor]];
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.