仅更改X轴值

4Ge*_*lOf 1 ios cgrect

我知道我可以像这样改变我的图像视图的位置

myImageView.frame = CGRectMake(99, 34, 32, 32);
Run Code Online (Sandbox Code Playgroud)

但是,如何更改x值,其余部分保留原样?我意识到这是一个简单的问题,但因为我不知道如何在搜索中找到解决方案.谢谢

sta*_*Man 8

//capture frame
CGRect thisRect = myImageView.frame;

//modify required frame parameter (.origin.x/y, .size.width/height)
thisRect.origin.x = 0;

//set modified frame to object
[myImageView setFrame:thisRect];
Run Code Online (Sandbox Code Playgroud)


Tia*_*ida 5

您对此问题有一些解决方案,但通常,如果您想简化,您最终会使用 UIView+Position ( UIView+Position.h UIView+Position.m ) 或UIView Helpers 之类的类别。虽然这可能有点矫枉过正。

另一种选择是使用 CGRect 辅助方法,如下所示:

myImageView.frame = CGRectOffset(myImageView.frame, 10, 0);
Run Code Online (Sandbox Code Playgroud)

有关此问题解决方案的更多信息,请阅读这篇文章