简体   繁体   中英

Cant resize a UIButton with CGRectMake

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: 在此处输入图片说明

Screenshot After Button Press: 在此处输入图片说明

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: 在此处输入图片说明

View Hierarchy: 在此处输入图片说明

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 

Note: For conditions not using Auto Layout, when views are embedded within a view the CGRectMake function will only be executed for the parent view and not for the child view.

If needed to perform the operation with CGRectMake , Auto Layout needs to be disabled

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM