Is this the best way to change the UIView framework for an existing view?

Suppose I want to temporarily change the view frame (for example, move it 10px to the right - perhaps due to the highlight of the change).

I assumed that psuedo code would be something like

self.someView.frame.origin.x += 10.0f;

But it gives me an error

lvalue required as left operand of assignment

So, instead, I create a new CGRect to represent the frame, change this CGRect, and then give the CGRect view as a frame

CGRect aFrame = self.someView.frame;
aFrame.origin.x += 10.0f;
[self.someView setFrame:aFrame];

It seems ridiculous that I can do the job on CGRect, but not on the view frame, which is CGRect.

So, is this the best way to change the frame for an existing view?

And for bonus points: why can't I assign a new value directly to the view property, why do I need to beat “around the bushes”?

thank

+3
3

: , " "?

: .

self.someView.frame.origin.x += 10.0f;

[[self someView] frame].origin.x += 10.0f;

, . [[self someView] frame] CGRect, , frame . - frame, -setFrame:. -setFrame CGRect.

+5

, . .

self.someView.frame - getter - . CGRect .

:

CGRect r = self.someView.frame;
r.origin.x += 10.0f;
self.someView.frame = r;

, , , , - . Objective C dot-notation , Obj-C , . , , self.someView.frame self.someView.frame() ( Pythonic).

( , Python ), , Obj-C. , , .

+2

I have a macro on github that will allow you to do this as

@morph(self.someView.frame, _.origin.x += 10);

Or if you want to change xandwidth

@morph(view.frame, {
    _.origin.x += 10;
    _.size.width -= 10;
});
0
source

Source: https://habr.com/ru/post/1769827/


All Articles