简体   繁体   中英

Cannot change the frame properties of a UIView

I have a UIView

@IBOutlet weak var myView: UIView!

I copy it using NSArchiver

var topview = NSKeyedUnarchiver.unarchiveObjectWithData(NSKeyedArchiver.archivedDataWithRootObject(self.myView));

I attempt to alter it using CGRectMake

topview?.frame = CGRectMake(0, 0, self.view.frame.width, self.view.frame.height * 1.5)

In response to the line above's '=' I receive the error

Cannot assign to result of this expression

I have attempted a variety of ways to alter topview's frame properties though each try is eventually met with this error. Any help would be greatly appreciated.

unarchiveObjectWithData returns an optional AnyObject and AnyObject itself does not have frame property. So you need to cast topView to UIView

var topview = NSKeyedUnarchiver.unarchiveObjectWithData(NSKeyedArchiver.archivedDataWithRootObject(self.myView)) as? UIView

In my understanding:

Because topview is an optional, frame is also optional. That means that there's a chance that frame is nil. So then you'd be assigning a CGRect to nil . Which is not possible.

This should work:

if let view = topview as? UIView {
    view.frame = CGRectMake(0, 0, self.view.frame.width, self.view.frame.height * 1.5)
}

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