2016-08-31 3 views
0

イメージにボタンがあります。UIView.animateWithDurationを使用して、ビューとイメージビューに新しいサイズを割り当てます。CGRectMakeでUIButtonのサイズを変更する

ビューが正しくスケールアップが、画像表示への変更

コードスニペットはありません:ボタンを押す前に

@IBAction func imageButtonDidTouch(sender: AnyObject) { 

    UIView.animateWithDuration(0.7) { 

     /*** Executes Properly ***/ 
     self.dialogView.frame = CGRectMake(0, 0, self.screenWidth!, self.screenHeight!) 

     /*** Does Not Execute ****/ 
     self.imageButton.frame = CGRectMake(0, 0, self.screenWidth!, 240) 


     /*** Executes Properly ***/ 
     self.likeButton.hidden = true 
     self.shareButton.hidden = true 
     self.userButton.hidden = true 
     self.headerView.hidden = true 
     self.dialogView.layer.cornerRadius = 0 

    } 

} 

スクリーンショット:ボタンを押した後 enter image description here

スクリーンショット: はenter image description here

画面の幅と画面H 8

screenWidth = UIScreen.mainScreen().bounds.width 
    screenHeight = UIScreen.mainScreen().bounds.height 

EDIT 1)のviewDidLoad(で定義されている: IMAGEBUTTON宣言

@IBOutlet weak var imageButton: UIButton! 

EDIT 2: 私はアニメーション機能に完了ハンドラを追加:

@IBAction func imageButtonDidTouch(sender: AnyObject) { 


    print("Height Before:", self.imageButton.frame.height) 
    print("Width Before:",self.imageButton.frame.width) 

    UIView.animateWithDuration(0.7, animations: { 

        /*** Executes Properly ***/ 
        self.dialogView.frame = CGRectMake(0, 0, self.screenWidth!, self.screenHeight!) 

        /*** Does Not Execute ****/ 
        self.imageButton.frame = CGRectMake(0, 0, self.screenWidth!, 240) 


        /*** Executes Properly ***/ 
        self.likeButton.hidden = true 
        self.shareButton.hidden = true 
        self.userButton.hidden = true 
        self.headerView.hidden = true 
        self.dialogView.layer.cornerRadius = 0 



     }) { (true) in 


      print("Height After:", self.imageButton.frame.height) 
      print("Width After:",self.imageButton.frame.width) 

    } 

} 

コンソールスクリーンショット: enter image description here

ビュー階層: enter image description here

+0

無効にする必要がself.imageButtonはnilですか? – Shubhank

+0

No.編集で追加されました –

+0

autolayoutが有効になっていますか? – azimov

答えて

1

チェックIMAGEBUTTONサイズの天気を設定かどうかの前に、あなたがscreenWidthを希望受けています。

UIScreenフレームデータを取得する適切な場所は、サブビューが画面にレイアウトされた後に呼び出されるため、viewDidLayoutSubviewsにあります。これは、端末の向きが変わるたびに呼び出されます。景観に入るmode.Thisが続いviewWillAppear:

override func viewDidLayoutSubviews() { 
    super.viewDidLayoutSubviews() 

    screenWidth = UIScreen.mainScreen().bounds.width 
    screenHeight = UIScreen.mainScreen().bounds.height 
    } 

後に呼び出されます: -

あなたはボタンプログラムによるを初期化し、宣言している場合は、次の

print(self.screenWidth) 

self.imageButton.frame.size = CGSizeMake(self.screenWidth,240) 

あなたは、制約のため自動レイアウトを使用している場合: -

  • @IBOutlet weak var widthConstraint: NSLayoutConstraint! 
    
  • その後、あなたのクラスであなたのボタンの幅の制約の@IBOutletを作成します。それに応じて設定してください。

    widthConstraint = self.screenWidth 
    

注:ビューがCGRectMake機能ビュー内に埋め込まれたときに、自動レイアウトを使用していない状態のため は、親ビューのためではなく、子ビューのために実行されます。

CGRectMakeを使用して操作を実行するために必要な場合は、自動レイアウトは

+0

はい。しかし、私は同じvar ** screenWidth **を使用していますが、ダイアログビューでも適切に縮尺されています –

+0

はい、結果は変わりません –

+0

このコードを変更しました:** self.imageButton.frame = CGRectMake(10、10、 self.screenWidth!、240)**ただし、imageButtonはまだ(0,0)のままです –

関連する問題