iOS navigation Bar Customization button causes the click Range to be too big problem

Source: Internet
Author: User

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];

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