gpt4 book ai didi

iphone - 将自定义角标(Badge)添加到导航栏中的分段控件

转载 作者:行者123 更新时间:2023-12-01 19:18:10 24 4
gpt4 key购买 nike

有一个UISegmentedControlnavigationBar ,创建于 Storyboard并拥有 Outlet连接到它。我尝试将自定义角标(Badge)添加到 UISegmentedControl ,但失败了。角标(Badge)不出现。

附言。 当我将其添加到 navigationBar 时,会出现自定义角标(Badge)( UISegmentedControl 的 super View ),但它是我的第二种方法。我想直接添加到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是第三方自定义角标(Badge) View 类。

最佳答案

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

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

关于iphone - 将自定义角标(Badge)添加到导航栏中的分段控件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11702159/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com