iphone UIBarButtonItem initWithImage - 点击时不突出显示

发布于 2024-09-16 22:26:50 字数 311 浏览 8 评论 0原文

我可以创建一个像这样的按钮,并且操作工作正常:

UIBarButtonItem *myBtn = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"image.png"] style:UIBarButtonItemStyleBordered target:self action:@selector(myMethod:)];

但是,当点击屏幕上的按钮时,不会显示向下状态突出显示。我已经尝试了一切。无法在带有自定义图像的 UIBarButtonItem 上突出显示吗?

I can create a button like this, and the action works fine:

UIBarButtonItem *myBtn = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"image.png"] style:UIBarButtonItemStyleBordered target:self action:@selector(myMethod:)];

However, when tapping the button on screen, no Down state highlight is displayed. I've tried everything. Can you not get a highlight on a UIBarButtonItem with a custom image?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

浊酒尽余欢 2024-09-23 22:26:50

尝试这样的事情(setShowsTouchWhenHighlighted: 是您正在寻找的):

UIImage* image = [UIImage imageNamed:@"image.png"];
CGRect frame = CGRectMake(0, 0, image.size.width, image.size.height);
UIButton* button = [[UIButton alloc] initWithFrame:frame];
[button setBackgroundImage:image forState:UIControlStateNormal];
[button addTarget:self action:@selector(myMethod:) forControlEvents:UIControlEventTouchUpInside];
[button setShowsTouchWhenHighlighted:YES];
UIBarButtonItem* barButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button];
[self.navigationItem setRightBarButtonItem:barButtonItem];
[barButtonItem release];
[button release];

Try something like this (setShowsTouchWhenHighlighted: is what you're looking for):

UIImage* image = [UIImage imageNamed:@"image.png"];
CGRect frame = CGRectMake(0, 0, image.size.width, image.size.height);
UIButton* button = [[UIButton alloc] initWithFrame:frame];
[button setBackgroundImage:image forState:UIControlStateNormal];
[button addTarget:self action:@selector(myMethod:) forControlEvents:UIControlEventTouchUpInside];
[button setShowsTouchWhenHighlighted:YES];
UIBarButtonItem* barButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button];
[self.navigationItem setRightBarButtonItem:barButtonItem];
[barButtonItem release];
[button release];
自控 2024-09-23 22:26:50

也许对大多数初学者来说更容易一些:

按钮创建代码

UIBarButtonItem *editButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Edit" style:UIBarButtonItemStyleBordered target:self action:@selector(editCells:)]; 
self.navigationItem.rightBarButtonItem = editButtonItem;
[editButtonItem release];

操作处理程序

- (void)editCells:(id)sender {
    UIBarButtonItem *buttonItem = (UIBarButtonItem *)sender;

    if (self.tableView.editing == YES) {
        self.tableView.editing = NO;
        buttonItem.title = @"Edit";
        buttonItem.style = UIBarButtonItemStyleBordered;        
    }
    else {
        self.tableView.editing = YES;
        buttonItem.title = @"Done";
        buttonItem.style = UIBarButtonItemStyleDone;
    }
}

解释代码

非常简单。我以编程方式创建按钮并将其添加到视图导航栏。当然,这仅在使用 NavigationController 时有效。

当用户单击按钮开始编辑相关表格视图时,该函数会检查表格编辑状态并更改按钮的文本和样式。

Maybe something a little bit easier for most beginners:

Code for Button Creation

UIBarButtonItem *editButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Edit" style:UIBarButtonItemStyleBordered target:self action:@selector(editCells:)]; 
self.navigationItem.rightBarButtonItem = editButtonItem;
[editButtonItem release];

Code for Action Handler

- (void)editCells:(id)sender {
    UIBarButtonItem *buttonItem = (UIBarButtonItem *)sender;

    if (self.tableView.editing == YES) {
        self.tableView.editing = NO;
        buttonItem.title = @"Edit";
        buttonItem.style = UIBarButtonItemStyleBordered;        
    }
    else {
        self.tableView.editing = YES;
        buttonItem.title = @"Done";
        buttonItem.style = UIBarButtonItemStyleDone;
    }
}

Explanation

Pretty straight forward. I create the button programatically and add it to the views navigation bar. Of course, this only works when using a NavigationController.

When the user clicks the button to start editing the related table view, the function checks the table editing state and changes the text of the button as well as the style.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文