1

uibuttonに設置したcontentModeがその通りになってくれません。
今回、contentModeのaspect fillの状態にしたいというのが望みです。
storyboard、コードで両方試してみましたが、うまくいきません。
uibuttonにsetImageで画像を設置し対処する方法は見つかりましたが、私の場合、uibuttonの画像の上に文字も表示したいので、setBackgroundImageにしなければならず、その対処が見つからず困っています。

ソースコード

class HomePageViewController: UIViewController {
    
    @IBOutlet weak var homeScrollView: UIScrollView!
    
    func createContentsView() -> UIView {
        let homeview = UIView()
        let homeview_width = homeScrollView.frame.width
        homeview.frame = CGRect(x: 0, y: 0, width: homeview_width, height: 1000)
        
        let margin = 28
        let space = 12
        let syusyoku = UIButton()
        syusyoku.setTitle("pasta and rice", for: UIControl.State.normal)
        let pi = UIImage(named: "hoge.jpg")
        syusyoku.imageView?.contentMode = .scaleAspectFill //ここの部分で試しています。
        syusyoku.contentHorizontalAlignment = .fill
        syusyoku.contentVerticalAlignment = .fill
        syusyoku.layer.cornerRadius = 15
        syusyoku.layer.masksToBounds = true
        syusyoku.setBackgroundImage(pi, for: .normal)
        syusyoku.setTitleColor(UIColor.white, for: .normal) // タイトルの色
        let child_width = (Int(homeview_width) - margin - space)/2
        syusyoku.frame = CGRect(x: margin, y: margin, width: child_width, height: child_width)
        homeview.addSubview(syusyoku)
        syusyoku.addTarget(self, action: #selector(buttonEvent(_:)), for: UIControl.Event.touchUpInside)

   func configureSV() {
            // scrollViewにcontentsViewを配置させる
            let subView = createContentsView()
            homeScrollView.addSubview(subView)

            // scrollViewにcontentsViewのサイズを教える
            homeScrollView.contentSize = subView.frame.size    
     }

     @objc func buttonEvent(_ sender: UIButton) {
        let storyboard: UIStoryboard = self.storyboard!
        let syusyoku = storyboard.instantiateViewController(withIdentifier: "CookedRiceandPasta") as! syusyokuViewController
        self.navigationController?.pushViewController(syusyoku, animated: true)
    }

1 件の回答 1

1

同様の質問が本家英語版のstackoverflowにありました。
UIButton's backgroundImage's content mode not respected

承認された回答によると、setBackgroundImage(_:for:)で指定された画像は内緒のUIIamgeViewに設定されるので、UIButton.imageViewを操作しても意味がないとのことです。

あなたがご質問に提示されたコードは不完全で実行することができないので、簡略化したコード例を挙げておきます。
(ご自分のプロジェクトにすぐに当てはめられるようなコード例を得たい場合には、「再現可能」なレベルのコードをご提示ください。)

class ViewController: UIViewController {
    
    weak var syusyokuButton: UIButton!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        let homeview_width = self.view.frame.width
        
        let margin = 28
        let space = 12
        let syusyoku = UIButton()
        syusyoku.setTitle("pasta and rice", for: UIControl.State.normal)
        let pi = UIImage(named: "hoge.jpg")
        //syusyoku.imageView?.contentMode = .scaleAspectFill //`syusyoku.imageView`は`setBackgroundImage`とは無関係
        syusyoku.contentHorizontalAlignment = .fill
        syusyoku.contentVerticalAlignment = .fill
        syusyoku.layer.cornerRadius = 15
        syusyoku.layer.masksToBounds = true
        syusyoku.setBackgroundImage(pi, for: .normal)
        syusyoku.setTitleColor(UIColor.white, for: .normal) // タイトルの色
        let child_width = (Int(homeview_width) - margin - space)/2
        syusyoku.frame = CGRect(x: margin, y: margin, width: child_width, height: child_width)
        self.view.addSubview(syusyoku)
        syusyokuButton = syusyoku
    }
    
    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        
        if let imageView = syusyokuButton.subviews.compactMap({$0 as? UIImageView}).first {
            imageView.contentMode = .scaleAspectFill //<-
        }
    }
    
}

既存のUI部品のsubviewsを解析して何かの動作をさせると言うのは、典型的なBad Practiceですが、現在提供されているUIButtonのAPIを使用する限り他に方法はなさそうです。

1
  • 意図通りにできました!ご丁寧にご指摘ありがとうございます。英語版にあったのですね。見落としていました。
    – Kaoruu_y
    2021年3月25日 11:10

この質問に回答するには、ログインする必要があります。

求めていた回答ではありませんか? のタグが付いた他の質問を参照する。