我刚刚学习 Swift 语言,对此我还很陌生。
我正在尝试创建一个带有包含 ImageView 的原型单元格的表视图。我希望“ImageView”根据 x 轴与“主故事板”居中。乍一看,一切都很好,直到我打开模拟器。打开模拟器后,我发现它绝对不以x轴为中心。 (我确信我的模拟器的手机类型和故事板的手机类型都是正确的。)
在这里,我想用代码将 imageView 居中。但我不知道应该把代码放在哪里。
我正在尝试放置此代码(如果可用)。 (我想应该是这样。)如果您有任何重新定位图像视图的 x 轴的建议,我完全开放。
let width = view.frame.size.width
var frm: CGRect = imageView!.frame
frm.origin.x = (width/2) - (frm.midX/2)
frm.origin.y = frm.origin.y
frm.size.width = frm.size.width
frm.size.height = frm.size.height
cell.imageView!.frame = frm
为了放置代码,我有两种类类型:“UITableViewCell”和“UIViewController”,我应该使用哪一种以及如何放置代码?
如何将 imageView 与该 tableView 内部对齐?
我将所有未完成的项目添加到此链接。这不是一个严肃的项目。只是一个为学习者服务的项目。
https://github.com/mmtbey/SwiftFirebase_InstaClone
登录:
用户名:[电子邮件受保护]
密码:123456
谢谢。
注意:我可以轻松对齐故事板上 imageView 的框架。然而,以 x 轴为中心实际上不起作用。当我运行模拟器时,我看到图像左对齐。我可以通过增加左线和 imageView 之间的间隙来解决这种情况。但这不是解决问题的正确方法。



< /a>



I have been just learning Swift language and I am pretty new at this.
I am trying to create a table view with protoype cell containing a ImageView. I want "ImageView" make centered according to x Axis with "main storyoard". At first glance, everything is good until I open the simulator. After opening the simualator, I see that it was absoulutely not centered on x-axis. (I am sure that my simulator's phone type and my storyboard's phone type is true.)
Here, I want to center the imageView with code. But I can't figure out where I should put the code.
I am trying to put this code if it is available. (I guess it should be.) If you have any suggestion to just relocate the x-axis of the imageview, I am totally open.
let width = view.frame.size.width
var frm: CGRect = imageView!.frame
frm.origin.x = (width/2) - (frm.midX/2)
frm.origin.y = frm.origin.y
frm.size.width = frm.size.width
frm.size.height = frm.size.height
cell.imageView!.frame = frm
In order to put the code, I am 2 class types: "UITableViewCell" and "UIViewController" Which one and how should I put the code?
How should align the imageView inside of the that tableView?
I added all unfinished project to this link. This is not a serious project. Just a project serving to learners.
https://github.com/mmtbey/SwiftFirebase_InstaClone
To sign in:
username: [email protected]
password: 123456
Thank you.
Note: I can align the frame of imageView on storyboard easily. However, centering on x-axis doesn't work in deed. When I run the simulator, I see the image left-aligned. I can fix the situation by inreasing the gap between left line and imageView. But that is not a proper way in order to solve the problem.







发布评论
评论(2)
由于您使用的是自动布局,我认为您不应该尝试使用框架来修复它。
我建议使用自动布局来修复它。
查看您的 Storyboard 图像,您似乎没有设置
centerX
和centerY
约束。操作方法如下:
这样做时,事情应该正确居中:
Since you are using auto layout, I don't think you should try to fix it using frames.
Fixing it with auto layout is what I recommend.
Looking at your Storyboard image, it does not seem like you are setting the
centerX
andcenterY
constraints.Here is how you do it:
On doing that, things should center properly:
我猜这里有一个错误,我找不到解决这种情况的方法。但是,我删除了 viewController 内的所有约束,并对约束进行了编码,如下所示。
祝你今天过得愉快。
I guess there is a bug here and I could not find a way to fix the situation. However, I deleted all constraits inside of the viewController and I coded the constraints like below.
Have a nice day.