我如何在导航栏上实现这一点

时间:2013-01-03 10:46:52

标签: iphone uinavigationcontroller uibutton uinavigationitem

我想设计我的navigationBar如下图,我怎样才能实现这一点。它可以在navigationBar上使用,或者我不需要navigationBar。 话题&设置必须是按钮,因为我想导航到另一个viewController
enter image description here

3 个答案:

答案 0 :(得分:2)

我不知道导航栏是否可行。

Tabbar

  • 你可以使用ALToolbar
  • 实现同样的目标

ALToolBar

klhorizontalselect

答案 1 :(得分:1)

使用自定义委托创建自定义导航栏..我使用此参见下面的示例..

创建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

然后创建委托类文件,如bellow ..

@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");
}

注意:这只是一个示例,此处根据您的要求实施您的逻辑。

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

我希望这对你有帮助......

答案 2 :(得分:0)

我这样做了,见下面的代码

//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];
相关问题