TabViewController设置UINavigationBar

时间:2014-09-11 01:10:01

标签: ios objective-c uinavigationbar

我正在尝试以编程方式设置我的UINavigationBar。我为界面构建器中的每个选项卡设置了NavBar,它继承自界面构建器中的GenericTabUiNavBar。

但是,当我运行应用程序时,GenericTabUiNavBar未将其元素加载到UINavBar中。

这是我的GenericTabUiNavBar代码:

·H

    #import <UIKit/UIKit.h>

    @interface GenericTabUiNavBar : UINavigationBar
    @property (nonatomic, strong) UILabel *firstLetter;
    @property (nonatomic, strong) UILabel *secondLetter; //need two labels

    @property (nonatomic,strong) UIButton *leftSideButton;

    @property (nonatomic,strong) UIButton *rightSideButton;

@end

的.m

@implementation GenericTabUiNavBar
@synthesize firstLetter;
@synthesize secondLetter;
@synthesize leftSideButton;
@synthesize rightSideButton;

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        // Initialization code
        //r g b
        //Yellow Color = 255,219,17
        UIColor *yellowColor = [[UIColor alloc] initWithRed:255 green:219 blue:17 alpha:1];
        UIColor *blueColor = [[UIColor alloc] initWithRed:1 green:129 blue:200 alpha:1];
        UIFont *fontFaceAndSize = [UIFont fontWithName:@"HelveticaNeue" size:36];

        //self
        self.frame = CGRectMake(0, 20, 320, 44);
        self.backgroundColor = blueColor;
        self.barTintColor = blueColor;

            //x.108 y.31
            //w.15 / y.21
        CGRect firstLetterRect = CGRectMake(108, 31, 15, 21);
        self.firstLetter = [[UILabel alloc] initWithFrame:firstLetterRect];
        firstLetter.text = @"An";
        firstLetter.font = fontFaceAndSize;
        firstLetter.textColor = [UIColor whiteColor];

            //128 31
            //22 21
        CGRect secLetterRect = CGRectMake(108, 31, 15, 21);
        self.secondLetter = [[UILabel alloc] initWithFrame:secLetterRect];
        secondLetter.text = @"APP";
        secondLetter.font = fontFaceAndSize;
        secondLetter.textColor = yellowColor;

        //tex pos =
        self.leftSideButton = [[UIButton alloc] initWithFrame:CGRectMake(20, 29, 30, 26)];
        self.rightSideButton = [[UIButton alloc] initWithFrame:CGRectMake(207, 29, 30, 26)];
        [leftSideButton setImage:[UIImage imageNamed:@"anImg.png"] forState:UIControlStateNormal];
        [rightSideButton setImage:[UIImage imageNamed:@"otherImg.png"] forState:UIControlStateNormal];

        [self addSubview:leftSideButton];
        [self addSubview:firstLetter];
        [self addSubview:secondLetter];
        [self addSubview:rightSideButton];

    }
    return self;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/

@end

在我的TabViewController中,我通过在界面构建器中将其挂起来调用它。为什么这不加载?

1 个答案:

答案 0 :(得分:0)

从storyboard或xib加载控制器时。 init方法

  

- (id)initWithCoder :( NSCoder *)解码器

调用

而不是

  

- (instancetype)initWithNibName:(NSString *)nibName                            bundle:(NSBundle *)nibBundle

至于观点,原因可能是相同的