In common iOS development, we often encounter such requirements as:
We need to customize the buttons to the right of the navigation bar, and the common custom wrapper buttons are as follows:
// set rightitem;
UIButton *btn = [UIButton buttonwithtype:uibuttontypecustom];
Btn. frame = cgrectmake(0, 0, + );
Btn. selected = NO;
[Btn settitle:@ " management " forstate:uicontrolstatenormal];
[Btn settitle:@ " cancel " forstate:uicontrolstateselected];
[Btn settitlecolor: [uicolor blackcolor] forstate: UIControlStateNormal];
[Btn addTarget:self action:@selector(tap:) forcontrolevents:uicontroleventtouchupinside];
uibarbuttonitem *rightitem = [[uibarbuttonitem alloc]initwithcustomview: BTN];
[self. Navigationitem setrightbarbuttonitem: Rightitem];
But in this way, we often can not adjust the custom uiview distance screen boundaries of the method, for some pit * Product managers, this is far from the heart of their loaded *, this time we can only be adjusted by the following method to the right edge of the CustomView distance value :
To set the rightbarbtnitem style :
UIButton *btn = [UIButton buttonwithtype:uibuttontypecustom];
Btn. frame = cgrectmake(0, 0, . );
[Btn setimage: [UIImage imagenamed:@ "Rightup"] forstate: UIControlStateNormal];
[Btn settitle:@ " recommended reward " forstate:uicontrolstatenormal];
[Btn settitlecolor: [uicolor lightgraycolor] forstate: UIControlStateNormal];
Btn. Titlelabel. Font = [uifont systemfontofsize: 15.0];
Btn. Titlelabel. TextAlignment = nstextalignmentright;
[Btn settitleedgeinsets:uiedgeinsetsmake(0, 0, 0)];
uibarbuttonitem *rewarditem = [[uibarbuttonitem alloc]initwithcustomview: BTN];
uibarbuttonitem *spaceitem = [[uibarbuttonitem alloc]initwithbarbuttonsystemitem: uibarbuttonsystemitemfixedspace target:nil action:nil];
Spaceitem. width =-;
[Btn addTarget:self action:@selector(pushtoreward) forcontrolevents: UIControlEventTouchUpInside];
self. Navigationitem. Rightbarbuttonitems = @[spaceitem,rewarditem];
Note: The emphasis here is to give Rightbarbuttonitems an array, adding a spaceitem element to control the distance to the right of the CustomView. The button within the Rightbarbuttonitem itself is the parent control, that is, the value of Rightbarbuttonitem is 5, and when we set the width to-15, The equivalent of the entire rightbarbuttonitem to the right to move 10, for special needs, you can adjust their own.
iOS about custom Rightbarbuttonitem