简体   繁体   English

iOS:按钮backgroundImage在注解标注视图中未更改

[英]iOS: Button backgroundImage not changing in Annotation Callout View

I have a MKMapView with a number of annotations representing stores. 我有一个带有许多表示商店的注释的MKMapView。 When the user taps on an annotation I present a custom annotation view. 当用户点击注释时,我会显示一个自定义注释视图。 This view contains a favourite button, that adds the store to the user's favourites. 该视图包含“收藏夹”按钮,该按钮将商店添加到用户的收藏夹中。

If the user presses this button it changes its backgroundImage property to another image indicating its status as a favourite store. 如果用户按下此按钮,则它将其backgroundImage属性更改为另一个图像,以指示其作为收藏夹商店的状态。

The problem that is occurring is that the image does not change when the user taps it. 出现的问题是用户点击图像时图像不会改变。 It is calling the method and adding the store to its favourites but it is not changing the image. 它正在调用该方法并将商店添加到其收藏夹中,但它没有更改图像。 If the user dismisses the callout and then taps the annotation again to view the callout, the button is then presenting the correct background image. 如果用户取消标注,然后再次点按注释以查看标注,则按钮将呈现正确的背景图像。

This problem only occurs on a iOS7 device but will work perfectly on an iOS7 simulator and iOS8 simulator/device. 此问题仅在iOS7设备上发生,但将在iOS7模拟器和iOS8模拟器/设备上完美运行。

Any help would be great, thanks. 任何帮助将是巨大的,谢谢。

are you using code like this one for change an image- 您是否正在使用像这样的代码来更改图片-

     [btn setBackgroundImage:img1 forState:UIControlStateNormal];
    [btn setBackgroundImage:img1 forState:UIControlStateSelected];
    [btn setBackgroundImage:img1 forState:UIControlStateHighlighted];

and try this one line when btnpressed 并在btnpressed时尝试这一行

[btn setNeedsDisplay]; [btn setNeedsDisplay];

First set 2 different images for two states of button. 首先为按钮的两种状态设置2张不同的图像。

[btn setBackgroundImage:img1 forState:UIControlStateNormal];
[btn setBackgroundImage:img2 forState:UIControlStateSelected];

then on buttonPress event 然后在buttonPress事件上

set

button.selected = !button.selected

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

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