2014-09-11 58 views
0

我想以编程方式设置我的UINavigationBar。我已经为界面构建器中的每个选项卡设置了NavBar,并且它在界面构建器中从我的GenericTabUiNavBar继承。TabViewController设置UINavigationBar

但是,当我运行应用程序时,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我在界面生成器钩住它调用它。为什么这不加载?

回答

0

控制器从故事板或xib加载时。 init方法

- (ID)的initWithCoder:(NSCoder *)解码器

被调用,而不是

- (instancetype)initWithNibName:(的NSString *)nibName 束:(NSBundle *)nibBundle

至于意见的原因可能是相同的

+0

这不起作用,“ - (id)initWithCoder:(NSCoder *)解码器”方法被击中,但导航栏属性不会改变... – Aziz 2014-09-11 22:31:10

+0

@Aziz还有一种方法可以将标签放在按钮旁边。尝试使用' - (void)setLeftBarButtonItems:(NSArray *)items animated:(BOOL)animated'将UIBarButtonItem作为label属性'enabled'设置为NO。或者仅仅因为它没有任何动作就离开它 – dopcn 2014-09-12 00:38:30

+0

我不想使用这些,我想添加我的自定义UIView元素.. – Aziz 2014-09-12 13:32:23

相关问题