zoukankan      html  css  js  c++  java
  • UINavigationController导航栏中添加多个UIBarButtonItem

    UINavigationController导航栏中添加多个UIBarButtonItem  

    2011-02-16 13:56:33|  分类: iPhone&iPad开发 |  标签:uinavigationcontroller  uibarbuttonitem  uitoolbar  |举报|字号 订阅

     
     

    (方法一)如果你使用自己的图片,那么就用这种方法:    

        UIView *leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 51, 32)];

            UIButton *leftButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 51, 32)];

            [leftButton setBackgroundImage:[UIImage imageNamed:@"back.png"]forState:UIControlStateNormal];

            [leftButton setBackgroundImage:[UIImage imageNamed:@"back_pressed.png"]forState:UIControlStateHighlighted];

            [leftView addSubview:leftButton];

            

    (方法二)如果你没有图片,只使用文字就用这种方法:    

            self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:leftView];

            

            

    (方法三)如果你的图片能完美的和UIBarButtonItem衔合,就用这种方法:    

            self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImageimageNamed:@"back.png"] style:UIBarButtonItemStylePlain target:selfaction:@selector(backActions:)];

     
    我建议还是用第一中方法。
    如果想使用更多地功能,往下看。
     
     
     
    在实际的开发中,导航器是最重要的容器之一,我们经常要在导航栏中添加各种样式的按钮,添加一个按钮很简单,代码如下:

    UIBarButtonItem *anotherButton = [[UIBarButtonItem alloc] initWithTitle:@"Setting" style:UITabBarSystemItemContacts 
                                                                     target:self action:@selector(clickSettings:)];          
    self.navigationItem.rightBarButtonItem = anotherButton; 
    [anotherButton release];

    其中按钮的样式可以有多种,具体的可以参考:https://developer.apple.com/library/ios/prerelease/#documentation/UIKit/Reference/UIBarButtonItem_Class/

    在有些项目中要在右面添加两个按钮,实现的样式如下图:

    8_101020162420_1.png

    实现的代码如下图:

    UIToolbar* tools = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 150, 45)]; 
    [tools setTintColor:[self.navigationController.navigationBar tintColor]]; 
    [tools setAlpha:[self.navigationController.navigationBar alpha]]; 
    NSMutableArray* buttons = [[NSMutableArray alloc] initWithCapacity:2];

    UIBarButtonItem *anotherButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd 
                            target:self action:@selector(clickSettings:)];

    UIBarButtonItem *anotherButton1 = [[UIBarButtonItem alloc] initWithTitle:@"Edit" style:UITabBarSystemItemContacts 
                                                            target:self action:@selector(clickEdit:)]; 
    [buttons addObject:anotherButton]; 
    [anotherButton release]; 
    [buttons addObject:anotherButton1]; 
    [anotherButton1 release]; 
    [tools setItems:buttons animated:NO]; 
    [buttons release]; 
    UIBarButtonItem *myBtn = [[UIBarButtonItem alloc] initWithCustomView:tools]; 
    self.navigationItem.rightBarButtonItem = myBtn;

    [myBtn release]; 
    [tools release];

    UIToolbar* tools = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 150, 45)]; 
    [tools setTintColor:[self.navigationController.navigationBar tintColor]]; 
    [tools setAlpha:[self.navigationController.navigationBar alpha]]; 
    NSMutableArray* buttons = [[NSMutableArray alloc] initWithCapacity:2];

    UIBarButtonItem *anotherButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd 
                            target:self action:@selector(clickSettings:)];

    UIBarButtonItem *anotherButton1 = [[UIBarButtonItem alloc] initWithTitle:@"Edit" style:UITabBarSystemItemContacts 
                                                            target:self action:@selector(clickEdit:)]; 
    [buttons addObject:anotherButton]; 
    [anotherButton release]; 
    [buttons addObject:anotherButton1]; 
    [anotherButton1 release]; 
    [tools setItems:buttons animated:NO]; 
    [buttons release]; 
    UIBarButtonItem *myBtn = [[UIBarButtonItem alloc] initWithCustomView:tools]; 
    self.navigationItem.rightBarButtonItem = myBtn;

    [myBtn release]; 
    [tools release];

    http://blog.sina.com.cn/s/blog_677089db0100um5p.html

    次方法验证,暂时不可行,没查明原因

    UIView * rightButtonParentView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 44)];
     

      rightButtonParentView.backgroundColor = [UIColor clearColor];
        
        int buttonSize = 32;
        int rightOffset = 20;
        UIButton * setButton = [[UIButton alloc] initWithFrame:CGRectMake(rightButtonParentView.frame.size.width - buttonSize - rightOffset, 6, buttonSize, buttonSize)];
        [setButton setBackgroundImage:[UIImage imageNamed:@"star.png"] forState:UIControlStateNormal];
        [setButton addTarget:self action:@selector(NSlog) forControlEvents:UIControlEventTouchUpInside];
        [rightButtonParentView addSubview:setButton];
        [setButton release];
        
        UIButton * searchButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 6, buttonSize, buttonSize)];
        [searchButton setBackgroundImage:[UIImage imageNamed:@"star.png"] forState:UIControlStateNormal];
        [searchButton addTarget:self action:@selector(NSlog) forControlEvents:UIControlEventTouchUpInside];
        [rightButtonParentView addSubview:searchButton];
        [searchButton release];
        
        
        UIBarButtonItem * rightButtonItem = [[UIBarButtonItem alloc] initWithCustomView:rightButtonParentView];
        [rightButtonParentView release];                            
        self.navigationItem.rightBarButtonItem = rightButtonItem;
        [rightButtonItem release];
     
     
     超出父视图大小 
  • 相关阅读:
    Windows Messenger 5.1 [Download from Microsoft]
    Building Web Parts for SPS读书笔记(2)
    Resources for KM & SharePoint Portal
    Microsoft.SharePoint.Menu Web Part [Free]
    忽略PNG透明区域的事件(AS/Flash)
    android webView 使用方法
    Android开发之Android开发规范(初)
    Android 下使用 JSON 实现 HTTP 请求
    html5 canvas 简单画板实现代码
    设定麦克风的声音品质
  • 原文地址:https://www.cnblogs.com/shupaopao/p/4332386.html
Copyright © 2011-2022 走看看