Re: [問題] ToolBar Button 自定圖片

看板MacDev作者 (wani)時間13年前 (2012/08/02 16:42), 編輯推噓0(000)
留言0則, 0人參與, 最新討論串2/2 (看更多)
這個問題我也碰過, 解決的方法應該搜尋的到, 不過大致用sample code講一下, 1. 先產生一個custom type的UIButton,並設定它的背景圖片 UIImage* imageButton = [UIImage imageNamed:@"你的圖片.png"]; UIButton* button = [UIButton buttonWithType:UIButtonTypeCustom]; [button setImage:imageButton forState:UIControlStateNormal]; [button setFrame:CGRectMake(0, 0, imageButton.size.width, imageButton.size.height)]; [button addTarget:self action:@selector(onclick:) forControlEvents:UIControlEventTouchUpInside]; 2. 第二步是關鍵,產生一個UIBarButtonItem,使用initWithCustomView,並把第一步 產生的UIButton塞進去 UIBarButtonItem* barButton = [[UIBarButtonItem alloc] initWithCustomView:button]; 接下來就是把這個barButton塞入UIToolBar,這樣應該就可以了 -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 61.218.106.220
文章代碼(AID): #1G6ZsI3s (MacDev)
文章代碼(AID): #1G6ZsI3s (MacDev)