Iphone 根据自定义按钮的状态设置其样式

Iphone 根据自定义按钮的状态设置其样式,iphone,objective-c,uibutton,Iphone,Objective C,Uibutton,我正在我的程序(不是IB)中创建一个自定义UIButton,我希望它在不同的状态下显示不同的图像,所以我做了: UIImage *learnImg=[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"01" ofType:@"png"]]; self.learn=[[UIButton buttonWithType:UIButtonTypeCustom] retain]; [self.learn set

我正在我的程序(不是IB)中创建一个自定义UIButton,我希望它在不同的状态下显示不同的图像,所以我做了:

UIImage *learnImg=[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"01" ofType:@"png"]];
self.learn=[[UIButton buttonWithType:UIButtonTypeCustom] retain];
[self.learn setFrame:CGRectMake(148, 108,254,97)]; 
[[self.learn imageView] setContentMode:UIViewContentModeScaleAspectFit];
[self.learn setImage:learnImg forState:UIControlStateNormal];

UIImage *learnSelected=[UIImage imageWithContentsOfFile:[[NSBundle mainBundle]   pathForResource:@"02" ofType:@"png"]];
[self.learn setImage:learnSelected forState:UIControlEventTouchDown];

[self.learn addTarget:self action:@selector(loadLearn:) forControlEvents:UIControlEventTouchUpInside];
问题是,这两个图像的大小不同,因此当我单击按钮时,按钮的位置被“平移”(我认为这是由于“设置帧”行为,因为xy位置应该是相对的)

我想不出办法来解决这个问题,我只想确保按钮在任何状态下都处于正确的位置。我是否可以为每个按钮状态(框架、图像等)设置一套完整的样式?还有其他想法吗

谢谢大家!

我最终通过两个操作事件重新设置按钮的中心来解决这个问题:UIControlEventTouchDown和UIControlEventTouchCancel。 谢谢大家的评论。

1。)尝试使用
setBackgroundImage:forState:
设置背景图像,这将拉伸图像以适应大小。如果您不想这样做,请尝试使用
imagedgeinsets
属性

2.)要为“ontouch”图像设置的状态为uicontrol状态高亮显示。如果您想使用“选定”状态(
selected
属性)进行自定义,则需要使用状态的组合(
UIControlStateSelected | uicontrolstatelogated
for selected+ontouch)


根据按钮状态给出图像名称。

UIControlEventTouchDown
不是按钮的状态。这是一个由按钮注册的事件。
ui按钮的各种状态如下

enum {
    UIControlStateNormal       = 0,                       
    UIControlStateHighlighted  = 1 << 0,                  // used when UIControl isHighlighted is set
    UIControlStateDisabled     = 1 << 1,
    UIControlStateSelected     = 1 << 2,                  // flag usable by app (see below)
    UIControlStateApplication  = 0x00FF0000,              // additional flags available for application use
    UIControlStateReserved     = 0xFF000000               // flags reserved for internal framework use
};
typedef NSUInteger UIControlState;
enum{
UIControlStateNormal=0,

UIControlStateHighlighted=1感谢您的评论。由于“UIViewContentModeScaleAspectFit”,图像的大小实际上在按钮上画得非常完美属性,并且我的意图是,当用户单击按钮时,按钮会“变大”。我是否可以将按钮的一个边缘固定到一个位置,并且每当按钮“变大”时,它不会弄乱整个位置?尝试设置按钮的
中心,并通过设置其边界使其变大。如果这还不够,您可以该层的
主播点乱七八糟
谢谢。我基本上什么都试过了,似乎什么都没用。具体来说,图像是两个锥形几何体,它们被转换成适合指定的空间。预期结果是,当用户单击按钮时,第二个较大的圆锥体将与第一个重叠(它们的顶点保持在同一位置)。简单地玩帧/边界等似乎太难实现。我想最简单的方法是创建更大的圆锥体作为图像并控制其“隐藏”属性?>\u谢谢,但这是我遇到的另一个问题。当我将图像设置为“UIControlStateHighlighted”状态时,按钮从未更改为我设置的图像,即使我还包括“uicontrol状态选择”。不知道发生了什么:在按钮操作中使用
button.selected=YES;
相应地更改按钮的状态,并查看它是否工作。
enum {
    UIControlStateNormal       = 0,                       
    UIControlStateHighlighted  = 1 << 0,                  // used when UIControl isHighlighted is set
    UIControlStateDisabled     = 1 << 1,
    UIControlStateSelected     = 1 << 2,                  // flag usable by app (see below)
    UIControlStateApplication  = 0x00FF0000,              // additional flags available for application use
    UIControlStateReserved     = 0xFF000000               // flags reserved for internal framework use
};
typedef NSUInteger UIControlState;