gpt4 book ai didi

objective-c - 如何将标题设置为 leftBarButtonItem

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:51:29 28 4
gpt4 key购买 nike

我有root ViewController 和detailed ViewController。当我推送到 detailedViewController 时,我得到带有根标题的 leftBarButtonItem。但我希望标题只是“返回”,仅此而已。那么该怎么做呢?

这没有帮助

self.navigationItem.leftBarButtonItem.title = @"Back";

创建我的类型 barButtonItem(例如 104 和左箭头)并将其设置为 leftBarButtonItem 是一个糟糕的决定。

除了在推送之前手动更改 rootViewController 的标题之外,还有其他方法吗?

最佳答案

来自 Apple 的文档:

backBarButtonItem

The bar button item to use when a back button is needed on the navigation bar.

@property(nonatomic, retain) UIBarButtonItem *backBarButtonItem Discussion

When this navigation item is immediately below the top item in the stack, the navigation controller derives the back button for the navigation bar from this navigation item. When this property is nil, the navigation item uses the value in its title property to create an appropriate back button. If you want to specify a custom image or title for the back button, you can assign a custom bar button item (with your custom title or image) to this property instead. When configuring your bar button item, do not assign a custom view to it; the navigation item ignores custom views in the back bar button anyway.

因此,您可以创建 barButtonItem(例如 – initWithTitle:style:target:action:)并将其分配给该属性。

此外,如果您想为 UIBarButtonItem(左侧或右侧)设置自定义图像,我建议您创建如下所示的类别扩展:

//UIBarButtonItem+Extension.h    
+ (UIBarButtonItem*)barItemWithImage:(UIImage*)image title:(NSString*)title target:(id)target action:(SEL)action;

//UIBarButtonItem+Extension.m
+ (UIBarButtonItem*)barItemWithImage:(UIImage*)image title:(NSString*)title target:(id)target action:(SEL)action
{
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(0.0, 0.0, image.size.width, image.size.height);
button.titleLabel.textAlignment = UITextAlignmentCenter;

[button setBackgroundImage:image forState:UIControlStateNormal];
[button setTitle:title forState:UIControlStateNormal];
[button addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];

UIBarButtonItem* barButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button];

return [barButtonItem autorelease];
}

然后将其用作

UIBarButtonItem* backBarButtonItem = [UIBarButtonItem barItemWithImage:[UIImage imageNamed:@"YoutImageName"] title:@"YourTitle" target:self action:@selector(doSomething:)];

关于objective-c - 如何将标题设置为 leftBarButtonItem,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9452079/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com