将自定义徽章添加到导航栏中的分段控件

时间:2012-07-28 15:09:08

标签: iphone ios uisegmentedcontrol badge

UISegmentedControl中有一个navigationBar,它在Storyboard中创建并且有Outlet连接到它。我曾尝试为UISegmentedControl添加自定义徽章,但失败了。徽章不会出现。

Ps。当我将自定义徽章添加到navigationBarUISegmentedControl的超级视图)时,会出现自定义徽章,但这是我的第二种方法。我想将它直接添加到UISegmentedControl,我可以吗?

MyTableViewController.h

...

@interface MyTableViewController : UITableViewController{
} 

@property (strong,nonatomic) IBOutlet UISegmentedControl  *segmentedControl;

...

MyTableViewController.m

@synthesize segmentedControl;

...

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    CustomBadge *customBadge = [CustomBadge customBadgeWithString:@"1" withStringColor:[UIColor whiteColor] withInsetColor:[UIColor orangeColor] withBadgeFrame:YES withBadgeFrameColor:[UIColor whiteColor] withScale:0.8 withShining:YES];

NSLog(@"self.segmentedControl :%@",self.segmentedControl);
NSLog(@"self.segmentedControl w: %f, h :%f",self.segmentedControle.frame.size.width, self.segmentedControlle.frame.size.height);
NSLog(@"customBadge x: %f, y: %f, w: %f, h :%f", customBadge.frame.origin.x, customBadge.frame.origin.y,customBadge.frame.size.width, customBadge.frame.size.height);

    [self.segmentedControl addSubview:customBadge];
}

...

记录结果:

self.segmentedControl :<UISegmentedControl: 0x3b7bf0; frame = (83 7; 154 30); opaque = NO; autoresize = RM+BM; layer = <CALayer: 0x3b7c70>>
self.segmentedControl w: 154.000000, h :30.000000
customBadge x: 0.000000, y: 0.000000, w: 20.000000, h :20.000000

CustomBadge是第三方自定义徽章视图类。

2 个答案:

答案 0 :(得分:3)

非常好!谢谢。现在,它可能是分段控件不是真正的视图,因为它的内容反映在子视图中(在mac上有这样的东西,比如标签视图) - 它们管理一系列视图,因此它们不会当他们在drawRect方法中绘制时,真的要关注子视图。因此,此控件可能会在其drawRect中覆盖您的徽章。你将不得不进一步探讨。

即便如此,还是有一个解决方案,即创建一个相同大小的容器UIView,首先添加分段控件,然后再添加自定义徽章,然后将该容器视图添加到UINavigationBar。这应该有用。

答案 1 :(得分:0)

我从未使用过CutomBadge,但我认为你需要设置框架。

相关问题