【问题标题】:setting custom UIButton style according to its state根据状态设置自定义 UIButton 样式
【发布时间】:2011-09-15 07:39:55
【问题描述】:

我正在我的程序中(不是在 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];

问题是,这两个图像的大小不同,所以当我点击按钮时,按钮的位置被“平移”(我相信这是由于“setFrame”行为,因为 xy 位置应该是相对的) .

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

谢谢!

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

【问题讨论】:

    标签: iphone objective-c uibutton


    【解决方案1】:

    UIControlEventTouchDown 不是按钮的状态。它是由按钮注册的事件。 UIButton 的各种状态是

    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;
    

    【讨论】:

    • 谢谢,但这是我遇到的另一个问题。当我将图像设置为“UIControlStateHighlighted”状态时,按钮永远不会更改为我设置的图像,即使我还包含了“UIControlStateSelected”。不知道发生了什么:S
    • 尝试在按钮操作中使用button.selected = YES;相应地更改按钮的状态,看看它是否有效。
    【解决方案2】:

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

    2.) 您希望“ontouch”图像的状态是 UIControlStateHighlighted。如果你想做某事。自定义具有“选定”状态(selectedproperty),您需要使用状态组合(UIControlStateSelected|UIControlStateHighlighted for selected+ontouch)

    【讨论】:

    • 感谢您的评论。由于“UIViewContentModeScaleAspectFit”属性,图像的大小实际上已经完美地绘制在按钮上,而且当用户单击它时按钮“变大”也是我的意图。无论如何我可以将按钮的一个边缘固定到一个位置,并且每当它“增长”时就不会弄乱整个位置?
    • 尝试设置按钮的center 并通过设置其边界使其更大。如果这还不够,你可以弄乱图层的anchorPoint
    • 谢谢。我基本上尝试了一切,似乎没有任何工作。具体来说,图像是两个锥形几何形状,它们被转换以适应指定的空间。预期的结果是,当用户单击按钮时,第二个较大的圆锥与第一个重叠(它们的顶点保持在同一位置)。通过简单地使用框架/边界等来实现似乎太难了。猜猜最简单的方法是将更大的圆锥体创建为图像并控制其“隐藏”属性? >_
    【解决方案3】:
           UIButton *deleteButton = [UIButton buttonWithType:UIButtonTypeCustom];
            [deleteButton setFrame:CGRectMake(285, 5, 30, 30)];
            deleteButton.contentMode = UIViewContentModeScaleAspectFill;     
            UIImage *newImage12 = [UIImage imageNamed:@"x.png"];
            deleteButton.tag = indexPath.row;
            [deleteButton setImage:newImage12 forState:UIControlStateNormal];
            [deleteButton setImage:newImage12 forState:UIControlStateHighlighted];
            [deleteButton addTarget:self action:@selector(deleteRemindersMethod:) forControlEvents:UIControlEventTouchUpInside];
    

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

    【讨论】:

      猜你喜欢
      • 2011-01-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-12-27
      • 1970-01-01
      • 2021-11-30
      • 2010-09-29
      相关资源
      最近更新 更多