[英]iOS: Custom image UIBarButtonItem does not respond to touches
我正在嘗試將圖像用作導航欄中的按鈕。 按鈕顯示正常,但它們不響應觸摸事件。 這是我設置按鈕的方式:
UIImageView *iv = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"arrow_up_24.png"]];
iv.userInteractionEnabled=YES;
UIBarButtonItem * logoutButton = [[UIBarButtonItem alloc] initWithCustomView:iv ];
logoutButton.target=self;
logoutButton.action=@selector(logoutButtonPressed);
我錯過了什么?
如果我沒記錯的話,我在過去的一個項目中遇到了這個問題。 我相信這是UIBarButtonItem
一個問題。 解決方法是......
UIButton *imageButton = [UIButton buttonWithStyle:UIButtonStyleCustom];
imageButton.frame = CGRectMake(0,0,24,24);//Standard size of a UIBarButtonItem i think.
[imageButton setImage:[UIImage imageNamed:@"arrow_up_24.png"] forState:UIControlStateNormal];
[imageButton addTarget:self action:@selector(logoutButtonPressed) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *barItem = [[UIBarButtonItem alloc] initWithCustomView:imageButton];
//將它添加到您的欄或此處的任何內容。
如果你想像常規按鈕一樣白色發光,你必須設置
imageButton.showsTouchWhenHighlighted = YES;
試試這個,我認為問題是你正在設置圖像對象而不是按鈕對象。
UIButton *navBarButton = [[UIButton alloc] init];
[navBarButton setImage:[UIImage imageNamed:@"arrow_up_24.png"] forState:UIControlStateNormal];
[navBarButton addTarget:self action:@selector(logoutButtonPressed) forControlEvents:UIControlEventTouchUpInside];
// Use self.navigationItem.leftBarButtonItem if that's your preference
UIBarButtonItem *rightItem = [[UIBarButtonItem alloc] initWithCustomView:rightItemImage];
self.navigationItem.rightBarButtonItem = rightItem;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.