简体   繁体   中英

setting frame in swift doesn't work as expected

I'm porting an app from Objective-C into pure Swift and I'm facing strange problem.

I've got AlertView class which is replacement for standard UIAlertView (now UIAlertController ) for displaying animatable popups. AlertView is created in UIViewController extension - it's inited with view controller's view frame and added as a subview.

AlertView has a property which is a PopupView 's instance - custom UIView subclass with xib (on Autolayout). This popup should has dynamic height depends on its contents (multiline message label).

Now when I'm trying to animate this popup in AlertView class:

  • when I set in PopupView setTranslatesAutoresizingMaskIntoConstraints(false) - view's height is correct but setting its frame in animation doesn't work as expected - view is sticked to the top left corner
  • when I set setTranslatesAutoresizingMaskIntoConstraints(true) - animation works as expected BUT view has a size from xib (won't expand according to contents)

What can be wrong here?

EDIT Showing popup method:

private func showPopup(popupView: PopupView)
{
    var beginFrame = popupView.frame
    beginFrame.origin.y = -beginFrame.size.height
    beginFrame.origin.x = self.bounds.size.width/2 - beginFrame.width/2
    popupView.frame = beginFrame

    var endFrame = beginFrame
    endFrame.origin.y = self.bounds.size.height/2 - endFrame.size.height/2

    popupView.hidden = false
    DLog(beginFrame)

    UIView.animateWithDuration(kAnimationTime, delay: 0, usingSpringWithDamping: kAnimationDamping, initialSpringVelocity: kAnimationSpringVelocity, options: UIViewAnimationOptions.CurveEaseIn, animations:
        { () -> Void in
            DLog(endFrame)
            popupView.frame = endFrame
    }, completion: nil)
}

in both cases it shows in console:

(72.5, -155.0, 230.0, 155.0)

(72.5, 256.0, 230.0, 155.0)

EDIT2

setTranslatesAutoresizingMaskIntoConstraints(false)

设置假

setTranslatesAutoresizingMaskIntoConstraints(true)

设置真

Ok, got solution. I've stopped mixing autolayout and direct frame modifications and use pure autolayout instead.

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