Ios 使用obj-c的UINavigationController居中按钮
我试图在导航栏标题中添加一个居中按钮,但对如何操作感到困惑Ios 使用obj-c的UINavigationController居中按钮,ios,objective-c,uibutton,uikit,Ios,Objective C,Uibutton,Uikit,我试图在导航栏标题中添加一个居中按钮,但对如何操作感到困惑 - (void)viewDidLoad { [super viewDidLoad]; UIImage *meImage = [[UIImage alloc] initWithContentsOfFile:[NSString stringWithFormat:@"%@/me.png", kSelfBundlePath]]; UIBarButtonItem *meButton = [[UIBarButtonItem alloc] i
- (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 *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");
}
你可以这样试试
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");
}
我认为这很简单,您可以使用UINavigationItem的titleView属性:
-(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;
}
我认为这很简单,您可以使用UINavigationItem的titleView属性:
-(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;
}
你想在中心添加按钮吗?是的,相当多你想在中心添加按钮吗?是的,相当多。谢谢你的帮助和其他提供类似答案的人。EP成功了,谢谢你的帮助和其他提供类似答案的人。我非常感谢你提供了它,看起来正是我想要做的&有一些额外的东西我很感激,谢谢你是的,我非常感谢你提供它,看起来正是我想要做的&有一些额外的东西我很感激,谢谢你