2014-09-11 69 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