In many app designs, the left/right side of the navigation bar will be designed to be an icon.
We usually take the form of creating a button:
Create button
UIButton *btn = [UIButton buttonwithtype:uibuttontypecustom];
[Btn setimage:[uiimage imagenamed:@ "Nav_item_game_icon"] forstate:uicontrolstatenormal];
[Btn setimage:[uiimage imagenamed:@ "Nav_item_game_click_icon"] forstate:uicontrolstatehighlighted];
[Btn SizeToFit];
Set the left button on the navigation bar
self.navigationItem.leftBarButtonItem = [[Uibarbuttonitem alloc] initwithcustomview:btn];
This sets an icon button to the left of the navigation bar.
But there is a problem:
The clickable range of this button is also too large.
Solution: Package a UIView for UIButton
Create button
UIButton *btn = [UIButton buttonwithtype:uibuttontypecustom];
[Btn setimage:[uiimage imagenamed:@ "Nav_item_game_icon"] forstate:uicontrolstatenormal];
[Btn setimage:[uiimage imagenamed:@ "Nav_item_game_click_icon"] forstate:uicontrolstatehighlighted];
[Btn SizeToFit];
Pack the UIButton into Uibarbuttonitem there will be button clicks too big problem
/solve this problem
uiview *containview = [[UIView alloc] initWithFrame: Btn.bounds];
[Containview addsubview:btn];
Set the left button on the navigation bar
self.navigationItem.leftBarButtonItem = [[Uibarbuttonitem alloc] initwithcustomview:containview];