Кнопка UINavigationController с центрированием с использованием obj-c

Я пытаюсь добавить центрированную кнопку в заголовок навигатора, смущенный тем, как я могу это сделать

- (void)viewDidLoad { [super viewDidLoad]; UIImage *meImage = [[UIImage alloc] initWithContentsOfFile:[NSString stringWithFormat:@"%@/me.png", kSelfBundlePath]]; UIBarButtonItem *meButton = [[UIBarButtonItem alloc] initWithImage:meImage style:UIBarButtonItemStylePlain target:self action:@selector(twitterButton)]; [self.titleView //i read this can be used to like set it but i havent been able to figure it out 

помощь будет оценена, довольно новая

проверьте этот код

 UIView *container = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 200, 44)]; container.backgroundColor = [UIColor clearColor]; UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom]; [btn setFrame:CGRectMake(0, 0, 44, 44)]; [btn setBackgroundImage:[UIImage imageNamed:@"button0.png"] forState:UIControlStateNormal]; [btn addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside]; [btn setShowsTouchWhenHighlighted:YES]; [buttonContainer addSubview:button0]; //add your spacer images and button1 and button2... self.navigationItem.titleView = container; 

Вы можете попробовать таким образом

 UIView *topView = [[UIView alloc]initWithFrame:CGRectZero]; UIButton * button = [[UIButton alloc]initWithFrame:CGRectZero]; [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside]; [button setTitle:@"Title here" forState:UIControlStateNormal]; [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; [button sizeToFit]; [topView addSubview:button]; [topView sizeToFit]; self.navigationItem.titleView = topView; -(void)buttonAction:(Id) sender { NSLog(@"button clicked"); } 

Я думаю, что это довольно прямо, вы можете использовать свойство titleView UINavigationItem:

  -(void)addCenterButton { UIImage *meImage = [[UIImage alloc] initWithContentsOfFile:[NSString stringWithFormat:@"%@/me.png", kSelfBundlePath]]; UIButton *button = [[UIButton alloc] initWithFrame::CGRectMake(0, 0, 40, 20)]; [button setImage:meImage forState:UIControlStateNormal]; [button addTarget:self action:@selector(twitterButton) forControlEvents:UIControlEventTouchUpInside]; self.navigationItem.titleView = button; }