2017-01-03 7 views
0

ObjCとxibを長年使って、Swift/Autolayoutに移行しました。私は非常にばかげた間違いを犯しました。私はボタンでHello Worldアプリケーションを持っています。スクリーンX N YiOS自動レイアウトボタンが表示されない

enter image description here

の途中、私は「として表示:」しようとすると、それはすべての画面iPhone 7P、7、SE、4Sのために途中で示したが...それは真ん中にあるボタンを示してい。しかし、ボタンは、ここで私はそれを示してどのように、色の背景に

@IBAction func buttonPresse(_ sender: AnyObject) { 
    let modalViewController = Modal1ViewController() 
    modalViewController.modalPresentationStyle = .overCurrentContext 
    present(modalViewController, animated: true, completion: nil) 
} 

そして、このようモーダル

override func viewDidLoad() { 
    super.viewDidLoad()   
    view.backgroundColor = UIColor.green 
    view.isOpaque = false 
} 

ザッツすべて!、なぜ私のボタンがあるの私のモーダルのViewController

に表示されていません表示されない:/ thx!

+0

ボタンのタイトルが空白の文字列に設定されていないか確認してください –

+0

こんにちは@RohitKP、タイトルはありません。文字列を他の値に変更しましたが、スクリーンショットで見られるようにタイトルはあります。 – MaKo

答えて

2

instantiateViewController(withIdentifier:)を使用してストーリーボードからインスタンスを作成する必要があるのではなく、Modal1ViewController()を使用して新しい空のインスタンスを表示しているという問題があります。だからこのようにしてみてください。

let modalViewController = self.storyboard?.instantiateViewController(withIdentifier: "Modal1ViewController") as! Modal1ViewController 
modalViewController?.modalPresentationStyle = .overCurrentContext 
present(modalViewController!, animated: true, completion: nil) 

注:あなたは、Storyboard IDチェックこのlinkを設定し、この質問が行うよう識別子を設定する方法を知っていない場合は、お使いのModal1ViewControllerStoryboard IDを設定する必要があり、このために

1

私たちが自動レイアウトを使用してUIを作成する場合、ストーリーボードはレイアウトとフレームのサブビューを保持するので、ストーリーボードで初期化する必要があります。ストーリーボードやXibの参照なしに初期化すると、インスタンスのみが作成され、自分の自己によって設定されていません。

のObjective-C: -

Modal1ViewController * vc = [self.storyboard instantiateViewControllerWithIdentifier:@"identifier"]; 

スウィフト: -

let vc = self.storyboard?.instantiateViewController(withIdentifier: "identifier") as! Modal1ViewController 
1

あなたはあなたがして、前の画面のボタンクリックで余分なタスクを実行したくない場合segueを直接使用して画面にリダイレクトし、UIViewControllerのPresentationプロパティを属性インスペクタからOver Current Contextに設定することができます。だから、あなたはこのすべてのことをする必要はありません。

Drag mouse pointer with right click pressed or press control key and drag mouse left key from button to second screen

Give segue from button to second screen

Then select Show option

Select Show from popup to assign segue

Then open Attribute Inspector and change as shown below

UIVIewController Attribute Inspector

関連する問題