Creating a UIBarButton Centered Programmatically(以编程方式创建居中的 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:以编程方式创建居中的 UIBarButton
基础教程推荐
- Android - 如何在runOnUiThread 中将数据传递给Runnable? 2022-01-01
- 在 iOS 7 下 CCMenu 错位 2022-01-01
- UINavigationBar 隐藏按钮文本 2022-01-01
- 从 UIWebView 访问元数据 2022-01-01
- EditText 中的 setHintTextColor() 2022-01-01
- 在视图控制器的宽度上水平均匀分布 UIButton 的最简单方法? 2022-01-01
- Firebase 云消息传递令牌未生成 2022-01-01
- iOS4 创建后台定时器 2022-01-01
- 更改 UITableView 部分标题的颜色 2022-01-01
- UINavigationItem 的持久 rightBarButtonItem 属性 2022-01-01