Add custom buttons and other uiviews to Navigation in iOS

Source: Internet
Author: User

The content is actually very simple, but sometimes we forget it. When we set rightBarButton/leftBarbutton, sometimes we find that the system is not very nice and we want to customize it. So we have this idea:

Directly add the Code as follows:

NSInteger width = [[UIScreen mainScreen] bounds]. size. width-20; // obtain the system width
UIView * view_title = [[UIView alloc] initWithFrame: CGRectMake (width, 10, 20, 20)]; // generate custom UIViewUIButton * btnItem = [UIButton alloc] initWithFrame: CGRectMake (0, 0, 20, 20)]; UIImage * image = [UIImage imageNamed: @ "down.png"]; [btnItem setImage: image forState: UIControlStateNormal]; [btnItem addTarget: self action: @ selector (favoriteBtnItem) forControlEvents: UIControlEventTouchUpInside];
[View_title addSubview: btnItem];
// Add the UIView to UIBarBtn

UIBarButtonItem * btn = [[UIBarButtonItem alloc] initWithCustomView: view_title];

// Set the button position on the right to self. navigationItem. rightBarButtonItem = btn;


Related Article

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: 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.