简体   繁体   中英

UIView frame not updating in Swift 2.0/iOS 8.0 & iOS 9.0

I am trying to update y position of a View in Swift 2.0. But it's not happening. I am not using Auto layout. Here is my code:

func textViewDidBeginEditing(textView: UITextView) {
    var frame : CGRect = self.frameView.frame
    frame.origin.y = -390
    //self.frameView.frame = frame
    self.frameView.frame = CGRectMake(frame.origin.x, frame.origin.y, frame.size.width, frame.size.height)
    print(" frame \(self.frameView.frame)")
}

Although when I print frame it shows the updated value but change does not reflect. Any clue what I am missing?

My guess is that some other code changes the frame back and overrides your change. This is why you do not see the frame where you expect it to be. Use view debug to check the frame (3 rectangles button just below your code tab in xcode). Also you could set a symbolic breakpoint on the UIView setFrame method to check from where the frame is changed.

Make sure you update constraints after you change frames of the UIView at run time. Hopefully it should work.

self.view .needsUpdateConstraints()
self.view .needsUpdateConstraints()
self.view .setNeedsLayout()
self.view .layoutIfNeeded()

OR If you are not using AutoLayout then check if you are setting right AutoResizing Mask on Storyboard. You need to update AutoResizingMask Programatically. Example:-

self.view.autoresizingMask = UIViewAutoresizingFlexibleBottomMargin |     UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin |     UIViewAutoresizingFlexibleTopMargin

我设置了视图的框架而不是frameView,问题已解决。

Updating the frame of view in func layoutSubviews() worked for me.

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