Lvalue需要作为赋值的左操作数

Ant*_*ton 1 tags objective-c uibutton uiscrollview ios

我想将suitSize分配给scrollButton我做错了什么?

UIView *scrollButton = [suitScrollView viewWithTag:1];
CGSize suitSize =CGSizeMake(10.0f,10.0f);
(UIButton *)scrollButton.frame.size=suitSize;
Run Code Online (Sandbox Code Playgroud)

Ste*_*her 5

frame是属性,而不是结构字段.您无法分配给它的子字段.把它想象成一个函数调用; 属性的点语法很方便.

这个:

scrollButton.frame.size = suitSize;
Run Code Online (Sandbox Code Playgroud)

相当于:

[scrollButton frame].size = suitSize;
Run Code Online (Sandbox Code Playgroud)

哪个不起作用; 分配给函数结果的字段没有任何意义.

相反,这样做:

CGFrame theFrame = [scrollButton frame];
theFrame.size = suitSize;
[scrollButton setFrame: theFrame];
Run Code Online (Sandbox Code Playgroud)

或者,如果您愿意:

CGFrame theFrame = scrollButton.frame;
theFrame.size = suitSize;
scrollButton.frame = theFrame;
Run Code Online (Sandbox Code Playgroud)

请注意,不必将scrollButton转换为UIButton; UIViews也有框架.