iPhone/iPad 弹性、可调整大小的按钮 - UIButton 子类化
我希望能够使用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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我自己修好了。对于那些有兴趣的人。
简单但有效。
I fixed it myself. For those who are interested.
Simple but effective.