Setting title for UINavigation bar in iphone

前端 未结 3 1312
滥情空心
滥情空心 2021-01-22 05:09

I am adding image to navigation bar it works fine but when i want to add title

   self.title=@\"Activity\";

it does not show anything

3条回答
  •  轮回少年
    2021-01-22 06:02

    You can simply hide default navigation bar. and add following code -

    NavigationView = [[UIView alloc]init];
    NavigationView.frame = CGRectMake(0, 0 , 320, 44);
    
    UIImageView *TopBarImg = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 320, 44)];
    TopBarImg.image = [UIImage imageNamed:@"Nav.png"];
    [NavigationView addSubview:TopBarImg];
    
    TopBarImg.userInteractionEnabled = TRUE;
    
    UILabel *TopTitle = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 320, 44)];
    TopTitle.backgroundColor = [UIColor clearColor];
    TopTitle.textAlignment = UITextAlignmentCenter;
    TopTitle.text = @"Activity";
    TopTitle.textColor=[UIColor whiteColor];
    TopTitle.backgroundColor=[UIColor clearColor];
    TopTitle.font=[UIFont fontWithName:@"Helvetica-Bold" size :18];
    [TopBarImg addSubview:TopTitle];
    
    UIButton *BackButton=[UIButton buttonWithType:UIButtonTypeCustom];
    BackButton.Frame = CGRectMake(5, 8, 46, 30);
    [BackButton setBackgroundImage:[UIImage imageNamed:@"back_btn.png"] forState:UIControlStateNormal];
    [BackButton setBackgroundImage:[UIImage imageNamed:@"back_btn_selected.png"] forState:UIControlStateHighlighted];
    [BackButton addTarget:self action:@selector(BackClicked) forControlEvents:UIControlEventTouchUpInside];
    [TopBarImg addSubview:BackButton];
    
    
    [self.view addSubview:NavigationView];
    

    And add one method for back Button click action as follows -

    -(void)BackClicked { [self.navigationController popViewControllerAnimated:YES]; }

提交回复
热议问题