iPhone/iPad 弹性、可调整大小的按钮 - UIButton 子类化

发布于 2024-09-28 06:18:03 字数 1262 浏览 2 评论 0原文

我希望能够使用scale9类型的背景图像制作具有图像背景的自定义按钮,这意味着按钮的宽度可以是动态的。我在网上看到了人们在每个按钮的基础上执行此操作的示例,但在我看来,创建一个新对象,该对象是 UIButton 的子类,然后您可以在 Interface Designer 中将其用作任何自定义的类,这不是更好吗?按钮(圆形矩形按钮设置为自定义)。

这是我到目前为止所拥有的。

#import <Foundation/Foundation.h>

@interface LargeButton : UIButton {
}

@end

#import "LargeButton.h"


@implementation LargeButton

- (id)initWithFrame:(CGRect)frame {
 if (self = [super initWithFrame:frame]) {

  self.frame = CGRectMake(0, 0, 170.0, 48.0);

  // Center the text vertically and horizontally
  self.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
  self.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;

  UIImage *image = [UIImage imageNamed:@"btnBigPurple.png"];

  // Make a stretchable image from the original image
  UIImage *stretchImage = 
  [image stretchableImageWithLeftCapWidth:15.0 topCapHeight:0.0];

  // Set the background to the stretchable image
  [self setBackgroundImage:image forState:UIControlStateNormal];

  // Make the background color clear
  //self.backgroundColor = [UIColor clearColor];
 }
 return self;
}

@end

但这似乎不起作用。当我运行这个即时通讯模拟器时,我看到按钮文本,但按钮没有背景。我已经放置了一个断点,我知道它正在运行并检查控制台并且没有错误。

有人可以帮忙吗?解决这个问题还是我的思维方式错误?

谢谢

I want to be able to make custom buttons which have an image background using a scale9 type background image meaning the width of the button can be dynamic. I have seen example on the web of people doing this on a per button basis but it seems to me that wouldn't it be better to create a new object which subclasses UIButton which you can then use in Interface Designer as the class for any custom button (round rect button set to custom).

Here is what I have so far.

#import <Foundation/Foundation.h>

@interface LargeButton : UIButton {
}

@end

#import "LargeButton.h"


@implementation LargeButton

- (id)initWithFrame:(CGRect)frame {
 if (self = [super initWithFrame:frame]) {

  self.frame = CGRectMake(0, 0, 170.0, 48.0);

  // Center the text vertically and horizontally
  self.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
  self.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;

  UIImage *image = [UIImage imageNamed:@"btnBigPurple.png"];

  // Make a stretchable image from the original image
  UIImage *stretchImage = 
  [image stretchableImageWithLeftCapWidth:15.0 topCapHeight:0.0];

  // Set the background to the stretchable image
  [self setBackgroundImage:image forState:UIControlStateNormal];

  // Make the background color clear
  //self.backgroundColor = [UIColor clearColor];
 }
 return self;
}

@end

This doesn't however seems to work. When I run this im simulator I see the button text but the button has no background. I have placed a breakpoint and I know its running and check the console and have no errors.

Can someone help? fix this or is my way of thinking wrong?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

青衫负雪 2024-10-05 06:18:03

我自己修好了。对于那些有兴趣的人。

#import <Foundation/Foundation.h>


@interface LargeButton : UIButton {
}

@end

#import "LargeButton.h"


    @implementation LargeButton

    - (void)drawRect:(CGRect)rect{
        UIImage *greenBalloon = [[UIImage imageNamed:@"btnBigPurple.png"] stretchableImageWithLeftCapWidth:10 topCapHeight:0];
        [self setBackgroundImage:greenBalloon forState:UIControlStateNormal];
    }

    @end

简单但有效。

I fixed it myself. For those who are interested.

#import <Foundation/Foundation.h>


@interface LargeButton : UIButton {
}

@end

#import "LargeButton.h"


    @implementation LargeButton

    - (void)drawRect:(CGRect)rect{
        UIImage *greenBalloon = [[UIImage imageNamed:@"btnBigPurple.png"] stretchableImageWithLeftCapWidth:10 topCapHeight:0];
        [self setBackgroundImage:greenBalloon forState:UIControlStateNormal];
    }

    @end

Simple but effective.

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