1

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

附言。当我将自定义徽章添加到navigationBar(的超级视图UISegmentedControl)时,会出现自定义徽章,但它是我的第二种方法。我想直接添加到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是第三方自定义徽章视图类。

4

2 回答 2

3

很好!谢谢。现在,可能是分段控件不是真正的视图,因为它的内容反映在子视图中(Mac上有这样的东西,比如选项卡视图) - 它们管理视图数组,因此它们不当子视图在 drawRect 方法中绘制时,它们并没有真正关注它们。所以这个控件可能会在它的drawRect中绘制你的徽章。你将不得不进一步调查。

尽管如此,还是有一个解决方案,就是创建一个大小相同的容器 UIView,先添加分段控件,然后再添加您的自定义徽章,然后将该容器视图添加到 UINavigationBar。那应该行得通。

于 2012-07-28T15:52:56.810 回答
0

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

于 2012-07-28T15:30:33.430 回答