为什么xib上不能把label放在button上

为什么xib上不能把label放在button上,第1张

UILabel *label = [[UILabel alloc] initWithFrame:(CGRect){100, 100, 100, 100}]

[label setBackgroundColor:[UIColor cyanColor]]//设置button的父视图时,父视图的颜色会把全部的子视图覆盖,使子视图看不见(但有时候又行,我也不知道是什么缘故)

[label.layer setBorderWidth:1.0f]

[label setUserInteractionEnabled:YES]//button的父视图开启交互开能让button有交互能力

[self.view addSubview:label]

UIButton *button = [[UIButton alloc] initWithFrame:(CGRect){15, 15, 70, 70}]

[button setContentEdgeInsets:(UIEdgeInsets){5, 5, 5, 5}]

[button setImage:[UIImage imageNamed:@"dog"] forState:UIControlStateNormal]

[button setBackgroundColor:[UIColor redColor]]

[button addTarget:self action:@selector(action:) forControlEvents:UIControlEventTouchUpInside]

[label addSubview:button]

选中xib中的label,在右边栏的第三个标签页中第三项是User Defined Runtime Attributes

添加一个keyPath,keyPath值为layer.borderWidth,类型为NSNumber,值为你想要设置的边框宽度。如图:

第二个是设置边框的颜色,为了兼容CALayer 的KVC ,你得给CALayer增加一个分类

@implementation CALayer (Additions)

- (void)setBorderColorFromUIColor:(UIColor *)color

{

  self.borderColor = color.CGColor

}

@end

这种方法只对选中的任意UIView有效,不可选中多个同时进行设置,只能一个一个进行添加,但可以选中Key Path列表中的内容进行复制和粘贴。

以上内容参考自:http://www.th7.cn/Program/IOS/201406/222942.shtml


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/bake/11743789.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-18
下一篇 2023-05-18

发表评论

登录后才能评论

评论列表(0条)

保存