UIToolbar 异常胖 UISegmentedControl
Posted
技术标签:
【中文标题】UIToolbar 异常胖 UISegmentedControl【英文标题】:UIToolbar with abnormally fat UISegmentedControl 【发布时间】:2013-02-13 02:57:06 【问题描述】:我从来没有在没有 Interface Builder 的情况下将分段控件添加到底部工具栏。我将相同的分段控件添加到导航控件工具栏而没有此问题。我究竟做错了什么?谢谢你。
//enable UIToolbar
self.navigationController.toolbarHidden = NO;
//UISegmentedControl
NSArray *segmentedControlItems = [[NSArray alloc] initWithObjects:@"Day One",@"Day Two",nil];
self.segmentedCon = [[UISegmentedControl alloc] initWithItems:segmentedControlItems];
[segmentedControlItems release];
[self.segmentedCon addTarget:self action:@selector(daySegConIndexChange) forControlEvents:UIControlEventValueChanged];
[self.segmentedCon setSelectedSegmentIndex:0];
[self.segmentedCon setSegmentedControlStyle:UISegmentedControlStyleBar];
[self.segmentedCon setWidth:85.0 forSegmentAtIndex:0];
[self.segmentedCon setWidth:85.0 forSegmentAtIndex:1];
[self.segmentedCon setFrame:[self.navigationController.toolbar bounds]];
//bar button items
UIBarButtonItem *flexibaleSpaceBarButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
UIBarButtonItem *segBarBtn = [[UIBarButtonItem alloc] initWithCustomView:self.segmentedCon];
//nsarray of tool bar items
NSArray *toolbarItems = [NSArray arrayWithObjects:flexibaleSpaceBarButton,segBarBtn,flexibaleSpaceBarButton,nil];
[self setToolbarItems:toolbarItems animated:YES];
[segBarBtn release];
【问题讨论】:
【参考方案1】:试试这个吧..
//UISegmentedControl
NSArray *segmentedControlItems = [[NSArray alloc] initWithObjects:@"Day One",@"Day Two",nil];
self.segmentedCon = [[UISegmentedControl alloc] initWithItems:segmentedControlItems];
[segmentedControlItems release];
[self.segmentedCon addTarget:self action:@selector(daySegConIndexChange) forControlEvents:UIControlEventValueChanged];
[self.segmentedCon setSelectedSegmentIndex:0];
[self.segmentedCon setSegmentedControlStyle:UISegmentedControlStyleBar];
//bar button items
UIBarButtonItem *flexibaleSpaceBarButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
UIBarButtonItem *segBarBtn = [[UIBarButtonItem alloc] initWithCustomView:self.segmentedCon];
【讨论】:
这行得通。谢谢你。我是遗漏了什么还是只有一行给我造成了这个麻烦?//this one
[self.segCon setFrame:[self.navigationController.toolbar bounds]];
这会将分段控件拉伸到工具栏的高度是有道理的,即将 self.segCon 的框架设置为导航工具栏的边界。每个索引的“setWidth:85.0”将其塞入左侧。【参考方案2】:
尝试设置分段控件的框架...
CGRect r = self.segmentedCon.frame;
self.segmentedCon.frame = CGRectMake(r.origin.x, r.origin.y, r.size.width, 30);
【讨论】:
不幸的是同样的结果。以上是关于UIToolbar 异常胖 UISegmentedControl的主要内容,如果未能解决你的问题,请参考以下文章
为依赖于其他项目的 Spring Boot 应用程序创建胖 jar 时出现异常