【问题标题】:how do i achieve this on navigationBar我如何在导航栏上实现这一点
【发布时间】:2012-12-17 17:19:05
【问题描述】:

我想像下图那样设计我的导航栏,我该如何实现。是否可以在导航栏上实现,或者我不需要导航栏。 主题和设置必须是按钮,因为我想导航到另一个 viewController

【问题讨论】:

标签: iphone uinavigationcontroller uibutton uinavigationitem


【解决方案1】:

不知道在导航栏上是否可以。

【讨论】:

    【解决方案2】:

    为此使用自定义委托创建自定义导航栏..我使用了这个,请参见下面的示例..

    创建CustomNavBar.h 文件,如下所示...

    //  Created by Paras on 03/12/11.
    //  Copyright 2010 __MyCompanyName__. All rights reserved.
    //
    
    #import <UIKit/UIKit.h>
    #import "CustomNavBarDelegate.h"
    
    @interface CustomNavBar : UIView {
    
        NSObject<CustomNavBarDelegate> *delegate;
    
        NSString *strCatOrLoc;
    
        UIButton *btnBack;
        UIButton *btnLeft;
    
        UIImageView *imgRightImage;
    
        UILabel *lbl;
    
        UIImageView *imgTitle;
    }
    @property (nonatomic, retain) UIButton *btnBack;
    @property (nonatomic, assign) NSObject<CustomNavBarDelegate> *delegate;
    -(void)onclickBack:(id)sender;
    - (id) initWithFrame: (CGRect)rect;
    -(void)setImage:(UIImage*)img NavTitle:(NSString *)title;
    -(void)setTitleImage:(UIImage*)img rightImage:(UIImage *)imgRight;
    -(void)setwithoutlogo:(UIImage*)img NavTitle:(NSString *)title;
    
    
    @end
    

    现在请看下面CustomNavBar.m文件的代码...

    //  Created by Paras on 03/12/11.
    //  Copyright 2010 __MyCompanyName__. All rights reserved.
    //
    
    #import "CustomNavBar.h"
    #import <QuartzCore/QuartzCore.h>
    #import "AppDelegate.h"
    
    @implementation CustomNavBar
    
    @synthesize delegate;
    @synthesize btnBack;
    #pragma mark -
    #pragma mark init methods
    - (id) initWithFrame: (CGRect)rect {
    
        if (self == [super initWithFrame:rect]) {
    
            //[self setBackgroundColor:[UIColor colorWithRed:241.0f/255.0f green:241.0f/255.0f blue:237.0f/255.0f alpha:1.0f]];
    //        [self setBackgroundColor:[UIColor whiteColor]];
    
            btnBack = [UIButton buttonWithType:UIButtonTypeCustom];
            btnBack.frame = CGRectMake(10.0, 8.0, 50.0, 28.0);
            [btnBack addTarget:self 
                        action:@selector(onclickBack:)
              forControlEvents:UIControlEventTouchDown];
    
            btnBack.layer.masksToBounds = YES;
            btnBack.layer.cornerRadius = 8.0;
            btnBack.layer.borderWidth = 0.5;
            btnBack.layer.borderColor = [[UIColor blackColor] CGColor];
            //btnBack.titleLabel.textColor = [UIColor blackColor];
    //        btnBack.titleLabel.text = @"Back";
            [btnBack.titleLabel setFont:Arial13];
            [btnBack setTitle:@"" forState:UIControlStateNormal];
            [btnBack setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    //        [btnBack setBackgroundColor:[UIColor colorWithRed:241.0f/255.0f green:241.0f/255.0f blue:237.0f/255.0f alpha:1.0f]];
            [btnBack setBackgroundColor:[UIColor clearColor]];
            [self addSubview:btnBack];
    
            btnLeft = [UIButton buttonWithType:UIButtonTypeCustom];
            btnLeft.frame = CGRectMake(280.0, 8.0, 310.0, 28.0);
            [btnLeft addTarget:self 
                        action:@selector(onclickLeft:)
              forControlEvents:UIControlEventTouchDown];
            [btnLeft setBackgroundColor:[UIColor clearColor]];
            [self addSubview:btnLeft];
    
        }
        return self;
    }
    
    -(void)setImage:(UIImage*)img NavTitle:(NSString *)title {
    
        imgRightImage = [[UIImageView alloc] initWithFrame:CGRectMake(0.0,0.0,320.0,44.0)];
        imgRightImage.contentMode = UIViewContentModeScaleAspectFill;
        imgRightImage.clipsToBounds = YES;
        imgRightImage.layer.masksToBounds = YES;
    //    imgRightImage.layer.cornerRadius = 11.0;
    //    imgRightImage.layer.borderWidth = 0.5;
        [imgRightImage setImage:img];
        [imgRightImage setBackgroundColor:[UIColor clearColor]];
        [self addSubview:imgRightImage];
        [imgRightImage release];
    }
    
    -(void)setwithoutlogo:(UIImage*)img NavTitle:(NSString *)title {
    
        imgRightImage = [[UIImageView alloc] initWithFrame:CGRectMake(275.0,2.0,40.0,40.0)];
        imgRightImage.contentMode = UIViewContentModeScaleAspectFill;
        imgRightImage.clipsToBounds = YES;
        imgRightImage.layer.masksToBounds = YES;
        //    imgRightImage.layer.cornerRadius = 11.0;
        //    imgRightImage.layer.borderWidth = 0.5;
        //[imgRightImage setImage:img];
        [imgRightImage setBackgroundColor:[UIColor clearColor]];
        [self addSubview:imgRightImage];
        [imgRightImage release];
    
        lbl = [[UILabel alloc] initWithFrame:CGRectMake(70.0, 7.0, 180, 30.0)];
        //    lbl.font = [UIFont fontWithName:@"Arial" size:20.0];
        lbl.font = Arial16;
        lbl.numberOfLines = 1;
        lbl.tag = 11;
        lbl.backgroundColor = [UIColor clearColor];
        lbl.textColor = [UIColor blackColor];
        lbl.textAlignment = UITextAlignmentCenter;
        lbl.text = title;
        [self addSubview:lbl];
    }
    
    
    -(void)setTitleImage:(UIImage*)img rightImage:(UIImage *)imgRight {
    
        imgRightImage = [[UIImageView alloc] initWithFrame:CGRectMake(275.0,2.0,40.0,40.0)];
        imgRightImage.contentMode = UIViewContentModeScaleAspectFill;
        imgRightImage.clipsToBounds = YES;
        imgRightImage.layer.masksToBounds = YES;
        [imgRightImage setImage:imgRight];
        [imgRightImage setBackgroundColor:[UIColor clearColor]];
        [self addSubview:imgRightImage];
        [imgRightImage release];
    
        imgTitle = [[UIImageView alloc] initWithFrame:CGRectMake(68.0,3.0,200.0,38.0)];
        imgTitle.contentMode = UIViewContentModeScaleToFill;
    
        [imgTitle setBackgroundColor:[UIColor clearColor]];
        [imgTitle setImage:img];
        [self addSubview:imgTitle];
        [imgTitle release];
    }
    -(void)onclickLeft:(id)sender{
        NSLog(@">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>Nav LeftClick");
        [delegate btnleft_clicked:self];
    }
    -(void)onclickBack:(id)sender {
    
        NSLog(@">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>Nav BackClick");
        [delegate popViewController:self];
    }
    
    #pragma mark -
    #pragma mark
    - (void)dealloc {
        //[array release];
        [super dealloc];
    }
    
    @end
    

    然后创建如下所示的委托类文件..

    @class CustomNavBar;
    
    @protocol CustomNavBarDelegate
    
    
    @required
    
    - (void)popViewController:(CustomNavBar *)navBar;
    -(void)btnleft_clicked:(CustomNavBar *)navBar1;
    
    @end
    

    在您的课程中使用此代码后..例如..

    在 .h 文件中导入它,然后像下面这样使用..

    #import "CustomNavBarDelegate.h"
    @class CustomNavBar;
    @interface ViewController : UIViewController<CustomNavBarDelegate>
    {
        CustomNavBar *navBar;
    }
    - (void)popViewController:(CustomNavBar *)navBar1;
    @end
    

    并在.m 文件中定义该委托方法并创建和添加如下导航...

    - (void)viewDidLoad
    {
        navBar = [[CustomNavBar alloc] initWithFrame:CGRectMake(0, 0, 322, 44)];
        [navBar setDelegate:self];
        [self.view addSubview:navBar];
        [navBar setImage:[UIImage imageNamed:@"yourImageName"] NavTitle:@"yourTitle"];
    }
    - (void)popViewController:(CustomNavBar *)navBar1 {
    
        //  NSLog(@">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> delegate called");
        [self.navigationController popViewControllerAnimated:YES];
    }
    -(void)btnleft_clicked:(CustomNavBar *)navBar1{
        NSLog(@"\n\n btn Left Clicked InviteFriendsView");
    }
    

    注意:这只是一个例子,这里根据您的要求实现您的逻辑。

    在这里,您还可以在中间添加第三个按钮,并定义在 Delegate 和另一个 .m 文件中调用它的方法..

    希望对你有帮助...

    【讨论】:

      【解决方案3】:

      我是这样做的,请参见下面的代码

      //IvisionApps Button
          UIButton *ivisionButton= [UIButton buttonWithType:UIButtonTypeCustom];
          UIImage *ivisionButtonImage = [UIImage imageNamed:@"ivisionapps"];
          UIImage *ivisionButtonImagePressed = [UIImage imageNamed:@"ivisionappsSelected"];
          [ivisionButton setBackgroundImage:ivisionButtonImage forState:UIControlStateNormal];
          [ivisionButton setBackgroundImage:ivisionButtonImagePressed forState:UIControlStateHighlighted];
           [ivisionButton addTarget:self action:@selector(goIVisionApp) forControlEvents:UIControlEventTouchUpInside];
          ivisionButton.frame = CGRectMake(-8, -20, 106, 38);
          UIView *backButtonView = [[UIView alloc] initWithFrame:CGRectMake(-8, -20, 106, 38)];
          backButtonView.bounds = CGRectOffset(backButtonView.bounds, -14, -7);
          [backButtonView addSubview:ivisionButton];
          UIBarButtonItem *backBarButton = [[UIBarButtonItem alloc] initWithCustomView:backButtonView];
          self.navigationItem.rightBarButtonItem = backBarButton;
      
      
      
          //Auxilaries Button
      
          UIButton *auxiliariesButton= [UIButton buttonWithType:UIButtonTypeCustom];
          UIImage *auxiliariesButtonImage = [UIImage imageNamed:@"Auxiliaries"];
          UIImage *auxiliariesButtonImagePressed = [UIImage imageNamed:@"AuxiliariesSelected"];
          [auxiliariesButton setBackgroundImage:auxiliariesButtonImage forState:UIControlStateNormal];
          [auxiliariesButton setBackgroundImage:auxiliariesButtonImagePressed forState:UIControlStateHighlighted];
          [auxiliariesButton addTarget:self action:@selector(goBack) forControlEvents:UIControlEventTouchUpInside];
          auxiliariesButton.frame = CGRectMake(-19, -20, 106, 38);
          UIView *auxiliariesButtonView = [[UIView alloc] initWithFrame:CGRectMake(-19, -20, 106, 38)];
          auxiliariesButtonView.bounds = CGRectOffset(auxiliariesButtonView.bounds, -14, -7);
          [auxiliariesButtonView addSubview:auxiliariesButton];
          UIBarButtonItem *auxiliariesBarButton = [[UIBarButtonItem alloc] initWithCustomView:auxiliariesButtonView];
          self.navigationItem.leftBarButtonItem = auxiliariesBarButton;
      
          //hide backBarButton of NavigationItem
          [self.navigationItem setHidesBackButton:YES];
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2014-01-06
        • 2020-08-17
        • 2013-08-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多