在自定义 UIButton 中手动定位 titleLabel(使用layoutSubviews)
我已经为我的应用程序创建了一个 UIButton 子类,我需要手动将 titleLabel 重新定位到按钮高度的 1/4 左右。这篇文章; http://forums.macrumors.com/showthread.php?t=763140 似乎直接解决了同样的问题,并且解决方案很简单并且完全可以理解。但是,我未能实现此目的,因为我对 layoutSubviews 的重写从未被调用。我的按钮是静态的,因此只需要第一次调用layoutSubviews,但它从来都不是。我的代码:
@interface MyButton : UIButton {}
@implementation MyButton
- (id)initWithFrame:(CGRect)frame
{
self = [[UIButton buttonWithType:UIButtonTypeRoundedRect] retain];
[[self layer] setCornerRadius:14.0f];
[[self layer] setBorderWidth:3.0f];
[[self layer] setMasksToBounds:YES];
[self setBackgroundColor:[UIColor redColor]];
[self setFrame:frame];
return self;
}
- (void)layoutSubviews
{
NSLog(@"layout subs\n");
[super layoutSubviews];
}
@end
移动标签没有问题,但永远不会调用layoutSubviews。我也尝试过添加layoutIfNeeded,但没有什么区别。更奇怪的是,我尝试直接从构造函数调用[selflayoutSubviews],但layoutSubviews仍然没有被调用!我开始认为这甚至可能是 SDK 3.1.3 中的一个错误,
有人可以帮忙吗?!
i've create a UIButton subclass for my app and i need to manually reposition the titleLabel to be about 1/4 of the button's height. this post; http://forums.macrumors.com/showthread.php?t=763140 appears to directly address the same issue, and the solution is simple and perfectly understandable. however, i have failed to implement this because my override of layoutSubviews is never called. my button is static, so layoutSubviews only need be called the first time, but it never is. my code:
@interface MyButton : UIButton {}
@implementation MyButton
- (id)initWithFrame:(CGRect)frame
{
self = [[UIButton buttonWithType:UIButtonTypeRoundedRect] retain];
[[self layer] setCornerRadius:14.0f];
[[self layer] setBorderWidth:3.0f];
[[self layer] setMasksToBounds:YES];
[self setBackgroundColor:[UIColor redColor]];
[self setFrame:frame];
return self;
}
- (void)layoutSubviews
{
NSLog(@"layout subs\n");
[super layoutSubviews];
}
@end
moving the label would be no problem, but layoutSubviews is never called. i've also tried adding layoutIfNeeded, but it made no difference. what's even weirder, is i've tried to call [self layoutSubviews] directly from the constructor but layoutSubviews is still not called!. i'm starting to think this might even be a bug in SDK 3.1.3
can anyone help?!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在
layoutSubviews
中,您可以对子视图进行布局更改。确保方法内部:√ 调用
[super layoutSubviews]
x 不要更改
rect
的值代码> self.frame 在这里。如果这样做,它将递归调用layoutSubviews
。√ 确保仅更改其子视图的
框架
。来自 UIView 类参考
In
layoutSubviews
, you can do layout changes to your subviews. Make sure that inside the method:√ Call
[super layoutSubviews]
x Do not change value or
rect
ofself.frame
here. If you do so, it would recursively calllayoutSubviews
.√ Make sure you only change
frame
of its subviews.From UIView Class Reference
为了回答我自己的问题,我似乎错误地进行了子类化。采用以下方法可以正确调用layoutSubviews:
希望对其他人有帮助
to answer my own question, it seems i was subclassing incorrectly. taking the following approach results in layoutSubviews being called correctly:
hope that helps others