簡體   English   中英

iOS UIButton 圖像 contentMode 在 TouchUpInside 上閃爍

[英]iOS UIButton image contentMode flickering on TouchUpInside

如果我將其 imageView contentMode 設置為UIViewContentModeScaleAspectFill ,則 UIButton 圖像有問題。

UIButton *imageButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 200, 100)];
imageButton.imageView.contentMode = UIViewContentModeScaleAspectFill;
[imageButton setImage:[UIImage imageNamed:@"myImage.jpg"] forState:UIControlStateNormal];
[imageButton addTarget:self action:@selector(doSmth:) forControlEvents:UIControlEventTouchUpInside];

按鈕內部的圖像被適當縮放以填充整個按鈕區域。

單擊/觸摸按鈕后,圖像會調整大小(閃爍),就像將 contentMode 設置為UIViewContentModeScaleAspectFit

有誰知道如何在點擊/觸摸發生時消除這種閃爍?

謝謝!

為什么要設置按鈕 imageView 的內容模式? 您為指定狀態設置的圖像始終填充按鈕的大小。 所以你必須將按鈕的框架大小設置為圖像的大小,而不是設置imageView contentMode

閃爍可能是按鈕的突出顯示。 如果要設置突出顯示的自定義圖像,請使用以下代碼:

[imageButton setImage:buttonHightlightImage forState:UIControlStateHightlighted]

只需在imageButton attributes inspector禁用/取消選中highlighted adjusts image

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM