Supratik Majumdar Supratik Majumdar - 3 months ago 20
Swift Question

CGRectMake not executing for button inside view

I have a button with a image on it, I am using UIView.animateWithDuration to assign a new size to the view as well as the image view.

The view scales up correctly, but there is no change to the image view

Code Snippet:

@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

}

}


Screenshot Before Button Press:
enter image description here

Screenshot After Button Press:
enter image description here

Screen Width and Screen Height Have been Defined in viewDidLoad()

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


EDIT 1:
imageButton declaration

@IBOutlet weak var imageButton: UIButton!


EDIT 2:
I added a completion handler to the animate function:

@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)

}

}


Console Screenshot:
enter image description here

View Hierarchy:
enter image description here

Answer

Check before setting imageButton size wether or not are you receiving you desired screenWidth.

The right place to get the UIScreen frame data is in viewDidLayoutSubviews as it is called after the subViews have been laid out in screen also it is called after every time your device changes orientation such as your width will be different when your user goes into landscape mode.This is called after viewWillAppear:

override func viewDidLayoutSubviews() {
    super.viewDidLayoutSubviews()

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

Then:-

If you are initialising and declaring your button Programatically :

print(self.screenWidth)

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

If you are using AutoLayout for the constraints, then :-

  • Create an @IBOutlet of your button's width constraint in your class

    @IBOutlet weak var widthConstraint: NSLayoutConstraint!
    
  • Then set it accordingly.

     widthConstraint = self.screenWidth
    
Comments