我有下面的代碼將rightbarbuttonitemrightbarbuttonitem不顯示
UIButton* rightbutton = [UIButton buttonWithType:UIButtonTypeCustom];
[rightbutton setBackgroundImage:[UIImage imageNamed:@"share-icon.png"] forState:UIControlStateNormal];
[rightbutton addTarget:self action:@selector(share:) forControlEvents:UIControlEventTouchUpInside];
self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithCustomView:rightbutton] autorelease];
但它不顯示任何barbuttonitem。相反,如果我使用下面的代碼然後barbutton項目出現,但問題是我不能設置觸摸事件與此代碼barbuttonitem。
UIImageView *iconView=[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"dashboard-icon.png"]];
UIBarButtonItem *icon = [[UIBarButtonItem alloc] initWithCustomView:iconView];
self.navigationItem.leftBarButtonItem=icon;
[icon release];
[iconView release];
你能給示例代碼,請即如何做到這一點? –
我的答案包含示例代碼。你需要做的是聲明一個
image
變量並將其設置爲你的圖像。 – cxa感謝它的工作 –