简体   繁体   English

UIButton不会更改touchUpInside的背景

[英]UIButton does not change background on touchUpInside

I have some custom type UIButtons placed pragmatically in a custom view. 我在实用视图中实用地放置了一些自定义类型的UIButton。 The view is basically a menu. 该视图基本上是一个菜单。 I am generating the buttons with the following code, inside the view's .m file: 我正在视图的.m文件中使用以下代码生成按钮:

-(void) generateButtons: (NSMutableArray *) buttonsArray
{
   UIImage *barItemImage = [[UIImage imageNamed:@"ipad-menubar-button.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 5, 0, 5)];

   float buttonWidth = (self.frame.size.width - (2 * FIRSTBUTTONXCOORDINATE) - 25)/ 6.0f;
   float xCoord = FIRSTBUTTONXCOORDINATE;
   float yCoord = (self.frame.size.height - BUTTONHEIGHT) / 2.0f;
   for (int i = 0; i < buttonsArray.count; i++)
   {
       NSString* buttonTitle = [buttonsArray objectAtIndex:i];
       UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
       [button addTarget:self
               action:@selector(botButtonAction:)
        forControlEvents:UIControlEventTouchUpInside];

       [button setTitle:buttonTitle forState:UIControlStateNormal];
       button.frame = CGRectMake(xCoord, yCoord, buttonWidth, BUTTONHEIGHT);
       button.tag = i;
       [button setBackgroundImage:barItemImage forState:UIControlStateNormal];
       xCoord += buttonWidth + 5;
       [self addSubview:button];
   }
}

On touchUpInside, botButtonAction is called and it does what it is supposed to. 在touchUpInside上,botButtonAction会被调用,它会执行预期的操作。 However, the buttons do not usually give any visual indication of the touchUpInside. 但是,这些按钮通常不提供touchUpInside的任何视觉指示。 When I touchDown on a button and swipe in any direction, then I see the background color get darker. 当我按下按钮并向任意方向滑动时,我会看到背景颜色变深。 Sometimes, I see the same visual indication on touchUpInside. 有时,我在touchUpInside上看到相同的视觉指示。 It seems completely random though. 不过,似乎完全是随机的。

Any ideas as to what I might be doing wrong here? 关于我在这里可能做错了什么的任何想法?

Edit: Sorry, I got occupied with other issues. 编辑:对不起,我忙于其他问题。 Here is the code for botButtonAction and the method it calls 这是botButtonAction的代码及其调用的方法

-(void) botButtonAction: (UIButton *) sender
{
 if (self.delegate)
    [self.delegate optionsMenuAction:self data:sender.tag];
}

 -(void)optionsMenuAction:(OptionsMenueView *)view data:(int)tag
 {
     if (self.PDFVC)
     {
         [self.navigationController popToViewController:self animated:YES];
     }

     if (!self.optionsMenue.isAtWPC || tag != WPC)
         [self transitionFromViewAtIndex:self.selectedVCIndex toView:tag];

    else
         [self transitionFromViewAtIndex:self.selectedVCIndex toView:WPList];
}

But since I am using touchUpInside, these methods don't get called on touchDown. 但是由于我使用的是touchUpInside,因此不会在touchDown上调用这些方法。 I just want the button to be highLight (grayout the bg a little) on touchDown. 我只想在touchDown上将按钮设为highLight(将bg稍微变灰)。 As I state before, that happens on touchDown and swipe. 如我之前所说,这发生在touchDown和滑动上。

Edit I had so far been running this on actual devices (iPad air and iPad 2). 编辑到目前为止,我一直在实际设备(iPad air和iPad 2)上运行此程序。 When I tried running it on the simulator, I get the correct result. 当我尝试在模拟器上运行它时,我得到了正确的结果。 On mouse click, the button gets highlighted. 鼠标单击时,按钮将突出显示。

Edit I think I figured out the exact problem (still looking for a solution). 编辑我想我已经找到了确切的问题(仍在寻找解决方案)。 If I touchDown precisely on the button and the touch does not cover any part of the insets, then the highlighting works. 如果我恰好在按钮上按下了Down,并且触摸没有覆盖插图的任何部分,则突出显示起作用。 However, if the touch covers part of the button and also part of the insets/outside, then the highlighting doesn't work. 但是,如果触摸覆盖了按钮的一部分以及部分插入/外部,则突出显示将不起作用。

添加此行:

[button setBackgroundImage:HighlightImage forState:UIControlStateHighlighted];

put this code in botButtonAction: 将此代码放在botButtonAction中:

-(void)botButtonAction:(UIButton *)sender
{
    [sender setBackgroundImage:[UIImage imageNamed:@"some iamge.png"] 
                                                 forState:UIControlStateNormal];
}

or use the code below code in for loop 或在for循环中使用下面的代码

[button setBackgroundImage:[UIImage imageNamed:@"back_button_press.png"] forState: UIControlStateHighlighted];

or if you want to give some color then setcolour of button 或者如果您想提供一些颜色,则设置按钮的颜色

change the title colour when tap on the button like this 像这样点击按钮时更改标题颜色

[yourButon setTitleColor:[UIColor colorYouWant] forState:UIControlStateHighlighted];

hope this will help you...... 希望这个能对您有所帮助......

I had this problem at some point too. 我也有这个问题。 These other answers will give you the result you seek normally. 这些其他答案将为您提供通常需要的结果。 However.... You are using [UIButton buttonWithType:UIButtonTypeCustom].. Which is really buggy if you do not alloc/init the UIButton.. Try changing this line to a normal button allocation and then add the following lines of the answers in this post. 但是...您正在使用[UIButton buttonWithType:UIButtonTypeCustom]。如果您不分配/初始化UIButton,这确实是个问题。尝试将这一行更改为常规的按钮分配,然后在答案中添加以下几行这个帖子。 This should work for you. 这应该为您工作。

So in short, 简而言之,

replace [UIButton buttonWithType:UIButtonTypeCustom]; 替换[UIButton buttonWithType:UIButtonTypeCustom]; with [[UIButton alloc] init]; [[UIButton alloc] init];

And then add the following changes to your code to change the image. 然后将以下更改添加到您的代码以更改图像。

[myNewlyAllocatedButton setBackgroundImage:[UIImage imageNamed:@"back_button_press.png"] forState: UIControlStateHighlighted]; 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM