在自定义 UIButton 中手动定位 titleLabel(使用layoutSubviews)

发布于 2024-09-24 03:44:34 字数 1014 浏览 5 评论 0原文

我已经为我的应用程序创建了一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

最丧也最甜 2024-10-01 03:44:34

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 of self.frame here. If you do so, it would recursively call layoutSubviews.

Make sure you only change frame of its subviews.

From UIView Class Reference
From Apple

傻比既视感 2024-10-01 03:44:34

为了回答我自己的问题,我似乎错误地进行了子类化。采用以下方法可以正确调用layoutSubviews:

+ (id)buttonWithFrame:(CGRect)frame {
  return [[[self alloc] initWithFrame:frame] autorelease];
}

- (id)initWithFrame:(CGRect)frame {
  if (self = [super initWithFrame:frame]) 
  {
    ...
    [self setFrame:frame];
  }
  return self;
}

希望对其他人有帮助

to answer my own question, it seems i was subclassing incorrectly. taking the following approach results in layoutSubviews being called correctly:

+ (id)buttonWithFrame:(CGRect)frame {
  return [[[self alloc] initWithFrame:frame] autorelease];
}

- (id)initWithFrame:(CGRect)frame {
  if (self = [super initWithFrame:frame]) 
  {
    ...
    [self setFrame:frame];
  }
  return self;
}

hope that helps others

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文