如何在UIImagePickerController导航栏上更改按钮颜色?

时间:2022-11-20 14:03:33

I have managed to change the color of the navigation bar but the color of the buttons is still black. Is there any way to change the color of the buttons as well? See below image.

我设法改变了导航栏的颜色,但按钮的颜色仍然是黑色。有没有办法改变按钮的颜色?见下图。

UPDATE: Sorry had to remove the image due to copy right issue.

更新:由于版权问题,抱歉不得不删除图像。

10 个解决方案

#1


21  

It is not possible without your custom image.

没有您的自定义图像是不可能的。

but you should change the tint color as soon as you create the navigation controller:

但是您应该在创建导航控制器后立即更改色调颜色:

 UIImagePickerController *pickerImg = [[UIImagePickerController alloc] init];
pickerImg.navigationBar.barStyle = UIBarStyleBlackOpaque; // Or whatever style.
// or
pickerImg.navigationBar.tintColor = [UIColor whateverColor];

#2


1  

you can hide the naviagtion bar,and create a custom navagtion bar by using imageview and place 2 buttons on it.By doing this you can change the image/color of buttons as well as image view also as per your requirement.

您可以隐藏导航栏,并使用imageview创建自定义导航栏,并在其上放置2个按钮。通过这样做,您可以根据您的要求更改按钮的图像/颜色以及图像视图。

for hiding navigation bar:

隐藏导航栏:

 self.navigationController.navigationBarHidden=YES;

then in XIB : create a view of 44 height ,place imageview on it and place 2 button and 1 label on it.

然后在XIB中:创建一个44高度的视图,在其上放置imageview并在其上放置2个按钮和1个标签。

Hopely this will solve your problem.

希望这能解决你的问题。

#3


1  

@Ankit You can try getting the subviews from the navigation bar and then set the desired color . I used this approach to set the color of cancel button on the search bar . The size of the cancel button is 48*30 and you can use the subviews array to set the desired color. I am not sure but this may do the job . Below is the code i used to set the color of cancel button on the search bar.

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

#4


1  

If you want to affect the color of the buttons that appear in all views such as the one you are hoping to change, then simply use the following:

如果您想影响所有视图中出现的按钮的颜色,例如您希望更改的按钮的颜色,则只需使用以下内容:

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

#5


1  

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

#6


0  

You can achieve your desired customizations using the new UIAppearance APIs as of iOS 5 and onwards. This tutorial essentially shows you how.

从iOS 5及更高版本开始,您可以使用新的UIAppearance API实现所需的自定义。本教程基本上向您展示了如何。

#7


0  

You have to define a UIButtonTypeCustom button for your BarButtonItem.

您必须为BarButtonItem定义UIButtonTypeCustom按钮。

UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
UIBarButtonItem *yourBarButton = [[UIBarButtonItem alloc] initWithCustomView:button];

For more information, have a look at this post: UIBarButtonItem with color?

有关更多信息,请查看此帖子:带颜色的UIBarButtonItem?

#8


0  

I hope it will help you!

我希望它会对你有所帮助!

Step 1:

UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
[self presentViewController:picker animated:YES completion:NULL];

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

#9


0  

If you don't mind to use the same style across the whole app, use appearance as @Bourne suggested:

如果您不介意在整个应用程序中使用相同的样式,请使用@Bourne建议的外观:

    let font = UIFont(name:"Montserrat-Bold", size:16)!
    let attributes: LTDictStrObj = [
        NSForegroundColorAttributeName: UIColor.whiteColor(),
        NSFontAttributeName: font,
    ]
    UIBarButtonItem.appearance().setTitleTextAttributes(attributes, forState: UIControlState.Normal)

You can also change the background color of the navigation bar if you're using white color like me:

如果你像我一样使用白色,你也可以改变导航栏的背景颜色:

        picker.navigationBar.barTintColor = .redColor()

#10


0  

One liner using UIAppearance:

使用UIAppearance的一个班轮:

[[UINavigationBar appearanceWhenContainedInInstancesOfClasses:@[[UIImagePickerController class]]] setTintColor:[UIColor redColor]];

#1


21  

It is not possible without your custom image.

没有您的自定义图像是不可能的。

but you should change the tint color as soon as you create the navigation controller:

但是您应该在创建导航控制器后立即更改色调颜色:

 UIImagePickerController *pickerImg = [[UIImagePickerController alloc] init];
pickerImg.navigationBar.barStyle = UIBarStyleBlackOpaque; // Or whatever style.
// or
pickerImg.navigationBar.tintColor = [UIColor whateverColor];

#2


1  

you can hide the naviagtion bar,and create a custom navagtion bar by using imageview and place 2 buttons on it.By doing this you can change the image/color of buttons as well as image view also as per your requirement.

您可以隐藏导航栏,并使用imageview创建自定义导航栏,并在其上放置2个按钮。通过这样做,您可以根据您的要求更改按钮的图像/颜色以及图像视图。

for hiding navigation bar:

隐藏导航栏:

 self.navigationController.navigationBarHidden=YES;

then in XIB : create a view of 44 height ,place imageview on it and place 2 button and 1 label on it.

然后在XIB中:创建一个44高度的视图,在其上放置imageview并在其上放置2个按钮和1个标签。

Hopely this will solve your problem.

希望这能解决你的问题。

#3


1  

@Ankit You can try getting the subviews from the navigation bar and then set the desired color . I used this approach to set the color of cancel button on the search bar . The size of the cancel button is 48*30 and you can use the subviews array to set the desired color. I am not sure but this may do the job . Below is the code i used to set the color of cancel button on the search bar.

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

#4


1  

If you want to affect the color of the buttons that appear in all views such as the one you are hoping to change, then simply use the following:

如果您想影响所有视图中出现的按钮的颜色,例如您希望更改的按钮的颜色,则只需使用以下内容:

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

#5


1  

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

#6


0  

You can achieve your desired customizations using the new UIAppearance APIs as of iOS 5 and onwards. This tutorial essentially shows you how.

从iOS 5及更高版本开始,您可以使用新的UIAppearance API实现所需的自定义。本教程基本上向您展示了如何。

#7


0  

You have to define a UIButtonTypeCustom button for your BarButtonItem.

您必须为BarButtonItem定义UIButtonTypeCustom按钮。

UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
UIBarButtonItem *yourBarButton = [[UIBarButtonItem alloc] initWithCustomView:button];

For more information, have a look at this post: UIBarButtonItem with color?

有关更多信息,请查看此帖子:带颜色的UIBarButtonItem?

#8


0  

I hope it will help you!

我希望它会对你有所帮助!

Step 1:

UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
[self presentViewController:picker animated:YES completion:NULL];

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

#9


0  

If you don't mind to use the same style across the whole app, use appearance as @Bourne suggested:

如果您不介意在整个应用程序中使用相同的样式,请使用@Bourne建议的外观:

    let font = UIFont(name:"Montserrat-Bold", size:16)!
    let attributes: LTDictStrObj = [
        NSForegroundColorAttributeName: UIColor.whiteColor(),
        NSFontAttributeName: font,
    ]
    UIBarButtonItem.appearance().setTitleTextAttributes(attributes, forState: UIControlState.Normal)

You can also change the background color of the navigation bar if you're using white color like me:

如果你像我一样使用白色,你也可以改变导航栏的背景颜色:

        picker.navigationBar.barTintColor = .redColor()

#10


0  

One liner using UIAppearance:

使用UIAppearance的一个班轮:

[[UINavigationBar appearanceWhenContainedInInstancesOfClasses:@[[UIImagePickerController class]]] setTintColor:[UIColor redColor]];