I am using Swift iOS 8.0. I have the following UI setup for a UIViewController, which I have created using the Interface builder in Xcode 6.1.1.
Now let's take the top sub UIView (I will call it SubView1). It has a UIImageView within it. I want to do two things programmatically (I do not want to use AutoLayout):
Currently I have the following code which is attempting to do this (this has not been refactored yet):
override func viewDidAppear(animated: Bool) {
super.viewDidAppear(animated)
var mainViewWidth = self.view.bounds.width
var mainViewHeight = self.view.bounds.height
// calculate new size for subView
var subViewHeight : CGFloat = mainViewHeight / 4
var subViewWidth : CGFloat = mainViewWidth
var nextXPosn : CGFloat = 0
var nextYPosn : CGFloat = 0
// change frame size for SubView1
self.subView1.frame = CGRect(x: nextXPosn, y: nextYPosn, width: subViewWidth, height: subViewHeight)
var originalSubViewHeight : CGFloat = originalMainViewHeight / 4.0
var scaleFactor : CGFloat = subViewHeight / originalSubViewHeight
// calculate new height and width for image
var newHeight : CGFloat = subImage.bounds.height * scaleFactor
var newWidth : CGFloat = subImage.bounds.width * scaleFactor
// change frame size and position for image
self.subImage.frame = CGRect(x: 10, y: 10, width: newWidth, height: newHeight)
}
Please note that I have used arbitrary values for x and y positions. When this code executes neither does the size or the position change for subImage or subView1. Is there something I am not setting to allow for these views to change position and size? Why is nothing changing?
Note: what I just tried was to remove the line that changes the size for subView1
self.subView1.frame = CGRect(x: nextXPosn, y: nextYPosn, width: subViewWidth, height:
subViewHeight)
and the subImage size and position now changes. It seems as though by moving and changing the size of subView1, it prevents the change of size and position for subImage (it's child view). I am not sure why this is happening.
Try
self.subView1.frame = CGRectMake(x: nextXPosn, y: nextYPosn, width: subViewWidth, height: subViewHeight)
You were using CGRect, instead of using CGRectMake.
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.