在UIImagePickerController的导航栏中更改栏按钮的标题

时间:2013-07-15 20:01:56

标签: button navigation uiimagepickercontroller

我的问题很简单:如何在UIImagePickerController的导航栏中更改栏按钮的更改标题? 我为UIImagePickerController创建了控制器(由压力触发按钮),然后尝试修改其属性:

- (IBAction)chooseFromRoll:(id)sender {

    if ([UIImagePickerController isSourceTypeAvailable: UIImagePickerControllerSourceTypeSavedPhotosAlbum]){
        UIImagePickerController *imagePicker =
        [[UIImagePickerController alloc] init];

        imagePicker.navigationBar.tintColor = [UIColor colorWithRed:0.42 green:0.18 blue:0.66 alpha:1.0];
        //imagePicker.navigationItem.title = @"Scegli foto profilo";

        imagePicker.delegate = self;
        imagePicker.sourceType =
        UIImagePickerControllerSourceTypePhotoLibrary;
        imagePicker.mediaTypes = [NSArray arrayWithObjects:
                                  (NSString *) kUTTypeImage,
                                  nil];
        imagePicker.allowsEditing = NO;


        [imagePicker.navigationController.navigationItem.rightBarButtonItem  setTitle:@"Annulla"];
        [self presentViewController:imagePicker animated:YES completion:nil];
    }
}

但没有任何改变,只有导航栏被正确修改。

这是函数willShowViewController的代码:

- (void) navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated{

    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 320, 30)];
    label.textAlignment = 2;
    label.frame = CGRectMake(label.frame.origin.x, label.frame.origin.y, label.frame.size.width-200,label.frame.size.height);
    label.adjustsFontSizeToFitWidth = YES;
    label.font = [UIFont fontWithName:@"Futura" size:15.0];
    [label setFont:[UIFont boldSystemFontOfSize:16.0]];
    [label setBackgroundColor:[UIColor clearColor]];
    [label setTextColor:[UIColor whiteColor]];
    [label setText:@"Scegli foto profilo"];

    [navigationController.navigationBar.topItem setTitleView:label.viewForBaselineLayout];

    [navigationController.navigationBar.topItem.rightBarButtonItem setTitle:@"Annulla"];



}
你能告诉我哪里出错吗?

感谢

1 个答案:

答案 0 :(得分:5)

这是一个用于在uiimage选择器中添加导航栏标题的代码片段。

- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
UINavigationItem *ipcNavBarTopItem;

// add done button to right side of nav bar
UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithTitle:@"Photos"
                                                               style:UIBarButtonItemStylePlain 
                                                              target:self 
                                                              action:@selector(saveImages:)];

UINavigationBar *bar = navigationController.navigationBar;
[bar setHidden:NO];
ipcNavBarTopItem = bar.topItem;
ipcNavBarTopItem.title = @"Photos";
ipcNavBarTopItem.rightBarButtonItem = doneButton;
}

希望这会帮助你。

修改

第一个问题的答案是肯定的,你可以使用ibaction或者只是使用上面提供的方法创建按钮,两者都应该有效。如果您使用不作为,请确保在其中声明控制器,以便可以使其显示并从操作方法中将其关闭。第二个问题的答案如果你想要更改从相册中的图像选择器出现时呈现的视图的标题,那么你需要创建一个新的子视图并弹出它以允许用户选择所需的图像然后你可以像我上面那样改变标题。漫长的过程,我不会推荐它。第三个问题的答案,请尝试使用以下代码片段来声明按钮,

[btn.titleLabel setFont:[UIFont boldSystemFontOfSize:13]];

其中btn是按钮的名称,您可以修改字体名称和大小。我希望我的答案可以帮到你。祝你好运,如果你需要更多的帮助,请告诉我。