将 CustomBadge 添加到导航栏中的 UISegmentedControl [英] Add a CustomBadge to a UISegmentedControl in the navigationBar

查看:27
本文介绍了将 CustomBadge 添加到导航栏中的 UISegmentedControl的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

There is an UISegmentedControl in a navigationBar, which created in the Storyboard and have an Outlet wired to it. I have tried to add a custom badge to the UISegmentedControl, but failed. The badge does not appear.

Ps. 当我将它添加到 navigationBar(UISegmentedControl 的超级视图)时会出现自定义徽章,但它是第二个对我来说.我想直接将它添加到 UISegmentedControl 中,可以吗?

Ps. The custom badge appears when i add it to the navigationBar (the superview of the UISegmentedControl), but it is the second approach for me. I wanna add it directly to the UISegmentedControl, could I?

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 是第三方自定义徽章视图类.

the CustomBadge is the third party custom badge view class.

推荐答案

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

Very good! Thanks. Now, it COULD be that a segmented control is not a true view in the sense that its content is reflected in subviews (there are such things on the mac, like a tab view) - they manage an array of views, and thus they don't really pay any attention to subviews when they draw in the drawRect method. So this control may be drawing over your badge in its drawRect. You will have to probe further.

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

Even so, there is a solution, which is to create a container UIView of the same size, add the segmented control first, then add your custom badge second, then add that container view to the UINavigationBar. That should work.

这篇关于将 CustomBadge 添加到导航栏中的 UISegmentedControl的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆