将一个CustomBadge添加到navigationBar中的UISegmentedControl

navigationBar有一个UISegmentedControl ,它在Storyboard中创build,并有一个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是第三方自定义徽章视图类。

很好! 谢谢。 现在,它可能是一个分段控制不是一个真正的观点,因为它的内容反映在子视图(有这样的事情在Mac上,像一个选项卡视图) – 他们pipe理一系列的意见,因此他们不绘制drawRect方法时,不会真正关注子视图。 所以这个控件可能会在drawRect中绘制你的徽章。 你将不得不进一步探索。

即使如此,还是有一个解决scheme,就是创build一个相同大小的容器UIView,首先添加分段控件,然后添加自定义标记,然后将该容器视图添加到UINavigationBar。 这应该工作。

我从来没有使用CutomBadge,但我认为你需要设置框架。