iOS about custom Rightbarbuttonitem

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.