以编程方式创建居中的 UIBarButton [英] Creating a UIBarButton Centered Programmatically

查看:15
本文介绍了以编程方式创建居中的 UIBarButton的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一组条形按钮,我通常使用以下内容进行设置:

I have an array of bar buttons that I normally set with something like:

 NSArray *leftButtonArray = [[NSArray alloc]initWithObjects: backBarButton, separator1,postBarButton, separator1, memeBarButton, separator1, pollBarButton, nil];
self.navigationItem.leftBarButtonItems = leftButtonArray;

但是,我想将左侧的这些按钮更改为居中对齐.有谁知道我该怎么做?提供一个例子或使用我的按钮这样做会是一个加号.

However, I want to change these buttons on the left to instead be center aligned. Anyone know how I can do that? Providing an example or using my buttons to do so would be a plus.

谢谢!

推荐答案

据我了解,您的问题需要有一些按钮在左边,一些在中间,一些在右边,并带有相对分隔符.直接导航不会提供把按钮放在中间,或者在TitleView中

As I understand your problem it required to have some button to left, some on middle, and some on right with relative separator. Directly navigation will not provide to put button is middle, or in TitleView

您可以像这样为导航栏设置自定义标题视图.

You can set custom title view to the navigation bar like this way.

//To hide default back button
self.navigationItem.hidesBackButton=YES;

//Title View for Navigation
UIView *navTitle1 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 44)];
[navTitle1 setBackgroundColor:[UIColor lightGrayColor]];

//Left View button and separator
UIButton *backBarButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 50, 44)];
[backBarButton setTitle:@"Back" forState:UIControlStateNormal];

UIView *ttlview1 = [[UIView alloc] initWithFrame:CGRectMake(51, 0, 1, 44)];
[ttlview1 setBackgroundColor:[UIColor darkGrayColor]];


//Center view with two buttons and seprator for them
UIView *middleView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 121, 44)];
[middleView setBackgroundColor:[UIColor clearColor]];
[middleView setCenter:navTitle1.center];

UIButton *postBarButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 60, 44)];
[postBarButton setTitle:@"Post" forState:UIControlStateNormal];

UIView *ttlview2 = [[UIView alloc] initWithFrame:CGRectMake(middleView.frame.size.width/2, 0, 1, 44)];
[ttlview2 setBackgroundColor:[UIColor darkGrayColor]];

UIButton *memeBarButton = [[UIButton alloc] initWithFrame:CGRectMake((middleView.frame.size.width/2)+1, 0, 60, 44)];
[memeBarButton setTitle:@"Meme" forState:UIControlStateNormal];

[middleView addSubview:ttlview2];
[middleView addSubview:postBarButton];
[middleView addSubview:memeBarButton];

//Right button with seprator before that
UIView *ttlview3 = [[UIView alloc] initWithFrame:CGRectMake(self.view.frame.size.width-71, 0, 1, 44)];
[ttlview3 setBackgroundColor:[UIColor darkGrayColor]];

UIButton *pollBarButton = [[UIButton alloc] initWithFrame:CGRectMake(self.view.frame.size.width-70, 0, 50, 44)];
[pollBarButton setTitle:@"POLL" forState:UIControlStateNormal];

[navTitle1 addSubview:backBarButton];
[navTitle1 addSubview:ttlview1];
[navTitle1 addSubview:middleView];
[navTitle1 addSubview:ttlview3];
[navTitle1 addSubview:pollBarButton];
self.navigationItem.titleView = navTitle1;

看起来像,这个

也许这会对你有所帮助.

May be this would help you.

HTH,享受编码!

这篇关于以编程方式创建居中的 UIBarButton的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆