如何在 Swift 中移动 Table Cell 的 Prototype Cell 内部的 imageView

发布于 2025-01-11 18:19:12 字数 2280 浏览 3 评论 0 原文

我刚刚学习 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 之间的间隙来解决这种情况。但这不是解决问题的正确方法。

故事板视图

模拟器视图

我的约束和输出

ImageView< /a>

my_CellView

ContentView

TableView

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.

Storyboard view

simulator view

My constraints and output

ImageView

my_CellView

ContentView

TableView

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一片旧的回忆 2025-01-18 18:19:12

由于您使用的是自动布局,我认为您不应该尝试使用框架来修复它。

我建议使用自动布局来修复它。

查看您的 Storyboard 图像,您似乎没有设置 centerXcenterY 约束。

操作方法如下:

中心约束 swift iOS Storboard

自动布局约束 UITableView 中心图像在表视图单元格 swift iOS

这样做时,事情应该正确居中:

居中 UITableViewCell 图像swift iOS 故事板自动布局约束

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 and centerY constraints.

Here is how you do it:

Center constraints swift iOS Storboard

Autolayout constraints UITableView center image in table view cell swift iOS

On doing that, things should center properly:

Center UITableViewCell image swift iOS storyboard auto layout constraints

琴流音 2025-01-18 18:19:12

我猜这里有一个错误,我找不到解决这种情况的方法。但是,我删除了 viewController 内的所有约束,并对约束进行了编码,如下所示。

override func layoutSubviews() {
    super.layoutSubviews()
    
    
    
    let myWidth = contentView.frame.size.width - 90
    let myHeight = myWidth - 90
    
    
    UserEmailLabel.frame = CGRect(x: 30,
                                  y: 10,
                                  width: 200 ,
                                  height: 20)
    UserEmailLabel.textAlignment = NSTextAlignment.left
    
    
    imageView?.frame = CGRect(x: contentView.frame.size.width/2 - myWidth/2 ,
                              y: UserEmailLabel.frame.maxY + 20,
                                   width: myWidth,
                                   height: myHeight)
    
    CommentLabel.frame = CGRect(x: 30,
                                y: (imageView?.frame.maxY)!,
                                width: myWidth - 30 ,
                                height: contentView.frame.size.height/10)
    
    LikeLabel.frame = CGRect(x: myWidth + 20,
                             y: CommentLabel.frame.maxY,
                             width: 30,
                             height: contentView.frame.size.height/10)
    
    LikeLabel.textAlignment = NSTextAlignment.right
    LikeLabel.textAlignment = .right
    
    LikeLabel.font = .systemFont(ofSize: 20)
    
    LikeButton.frame = CGRect(x: 45,
                              y: CommentLabel.frame.maxY,
                              width: 40,
                              height: contentView.frame.size.height/10)
    
    
}

祝你今天过得愉快。

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.

override func layoutSubviews() {
    super.layoutSubviews()
    
    
    
    let myWidth = contentView.frame.size.width - 90
    let myHeight = myWidth - 90
    
    
    UserEmailLabel.frame = CGRect(x: 30,
                                  y: 10,
                                  width: 200 ,
                                  height: 20)
    UserEmailLabel.textAlignment = NSTextAlignment.left
    
    
    imageView?.frame = CGRect(x: contentView.frame.size.width/2 - myWidth/2 ,
                              y: UserEmailLabel.frame.maxY + 20,
                                   width: myWidth,
                                   height: myHeight)
    
    CommentLabel.frame = CGRect(x: 30,
                                y: (imageView?.frame.maxY)!,
                                width: myWidth - 30 ,
                                height: contentView.frame.size.height/10)
    
    LikeLabel.frame = CGRect(x: myWidth + 20,
                             y: CommentLabel.frame.maxY,
                             width: 30,
                             height: contentView.frame.size.height/10)
    
    LikeLabel.textAlignment = NSTextAlignment.right
    LikeLabel.textAlignment = .right
    
    LikeLabel.font = .systemFont(ofSize: 20)
    
    LikeButton.frame = CGRect(x: 45,
                              y: CommentLabel.frame.maxY,
                              width: 40,
                              height: contentView.frame.size.height/10)
    
    
}

Have a nice day.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文