2017-10-31 9 views
0

4つの四角いボタンを持つUIViewコントローラがあり、境界線を丸く設定しました。それはiPhone 8とiPhone Xでは完璧に機能しますが、iPhone SEとiPhone 8 PlusではUIButtonsはもう丸められません。私は、UIButtonsを正方形に設定し、自動レイアウトでその比率を維持するが、動作するようには見えない。私ViewController.SwiftすべてのデバイスでUIButtonsが正しく整形されていない

、私は4 UIButtonsをリンクされてきましたし、私は以下のように同じコードを適用した:

ここ
@IBOutlet weak var topLeftButtonImage: UIButton! 


// Edit it to round 
topLeftButtonImage.layer.cornerRadius = topLeftButtonImage.frame.size.width/2 
topLeftButtonImage.clipsToBounds = true 

// Add border 
topLeftButtonImage.layer.borderColor = UIColor.white.cgColor // Button border color 
topLeftButtonImage.layer.borderWidth = 4 // Button border width 

あなたはiPhone SEやiPhone上で動作を確認することができます8プラス。 iPhone 8とiPhone Xはともにです。

Screenshots iPhone Simulator

自動レイアウトの制約:

Auto-Layout in action

+0

のviewDidLayoutSubviews方法では、どのような 'func'は、このコードが実行されますか? – Flexicoder

+0

@Flexicoderの 'func viewDidLoad()' –

+0

のコードをfuncに移動しようとしました。viewDidAppear() –

答えて

3

設定ボタンcornerRadiusでのViewController

override func viewWillLayoutSubviews() { 
    super.viewWillLayoutSubviews() 
    // Edit it to round 
    topLeftButtonImage.layer.cornerRadius = topLeftButtonImage.bounds.size.height/2 
    topLeftButtonImage.clipsToBounds = true 

    // Add border 
    topLeftButtonImage.layer.borderColor = UIColor.white.cgColor // Button border color 
    topLeftButtonImage.layer.borderWidth = 4 // Button border width 
} 
+1

viewDidLayoutSubviewsは、コントローラのビューが更新、回転、または変更されるたびに呼び出されます。 –

+1

またはスクロールします。 –

関連する問題