dyllandry
dyllandry

Reputation: 113

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.

Upvotes: 0

Views: 332

Answers (2)

donnywals
donnywals

Reputation: 7601

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)
}

Upvotes: 0

Zell B.
Zell B.

Reputation: 10296

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

Upvotes: 2

Related Questions