Doug Smith
Doug Smith

Reputation: 29326

How do I create a UIView subclass with a fixed size?

I want to create a UIView subclass in order to have a specific UIView with some UILabels and a UIImageView in it. However, I want the UIView to be square and only ever 100x100pt. Is it possible to specify that the UIView should only ever be a specific size?

My only idea would be to override initWithFrame: and essential strip the values passed and throw them away, but that seems really wrong.

Upvotes: 0

Views: 1199

Answers (2)

virindh
virindh

Reputation: 3765

once the UIView loads, set its frame to be 100 x 100, there is no need to override anything. See the following:

self.frame = CGRectMake(self.frame.origin.x, self.frame.origin.y, 100, 100);

This takes away the need for overriding.

Upvotes: 1

Lance
Lance

Reputation: 9012

There's nothing wrong with overriding initWithFrame: as you described in your question. You would also want to override setFrame: to be sure nobody sets the frame that way either.

Another alternative, if you are using auto layout, would be to have your subclass add width and height constraints to itself that force it to be 100x100.

Upvotes: 2

Related Questions