ios - UiTableView 出现在 ViewController ios 上的错误位置

标签 ios xcode xamarin.ios storyboard

我目前正在开发一款应用程序,当按下按钮时,它会在按钮所在的 View Controller 上以模态方式呈现一个 View Controller 。这工作正常并且出现在正确的位置,但是,模态视图 Controller 包含一个 UiTableView,它似乎出现在 View Controller 的左上角,而不是靠近我将其放置在 Storyboard中的位置。我尝试添加所有约束,如下图所示,但没有成功。当我添加约束时,表格 View 根本不出现。

This is the image showing my Modal View Controller, With the table view being inside a view on the right hand side

Image showing where the UiTableView appears in relation to the location in storyboards

Constraints Added

Result After Constraint

希望有人可以提供帮助,因为我已经走进了死胡同。

谢谢 杰米

最佳答案

好的,看起来您是使用 Storyboard 来制作 UI 的。没关系,但您应该知道还有另一种创建 UI 元素(tableView、按钮等)的方法:以编程方式。

因为您使用了 Storyboard,您可能忘记选中/取消选中其中的某些设置,这可能会导致您遇到问题,我可能无法轻松诊断您的问题。因此,我将向您展示如何以编程方式解决此问题。

以编程方式

您可以在您尝试呈现的 viewController 中以编程方式设置 tableView 的约束。

因此,如果要显示的 viewController 名为 viewController,而 tableView 为 tableView,则应具有以下内容:

class viewController: UIViewController {
    @IBOutlet weak var tableView: UITableView!

    override func viewDidLoad() {
        super.viewDidLoad()


        /* First, tell XCode that you want to manually set the tableView's constraints using AutoLayout */

        tableView.translatesAutoresizingMaskIntoConstraints = false
        self.addSubview(tableView)

        /* setup constraints for your tableView */
        tableView.centerXAnchor.constraint(equalTo: self.centerXAnchor).isActive = true
        tableView.centerYAnchor.constraint(equalTo: self.centerYAnchor).isActive = true
    }
}

创建包含 tableView 的 viewController 的另一种(更简单)方法是让 viewController inherit 来自 UITableViewController 而不是 UIViewController .这是一个简洁的小继承技巧,您可以使用它来使您的代码更加简洁并避免编写不需要编写的代码。如果您从 UITableViewController 继承,您会自动在 viewController 中获得一个“免费”的 tableView,而无需创建一个新的:

class viewController: UITableViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
    }
}

通常,如果我们只打算在其中包含一个 UI 元素:tableView,我们会从 UITableViewController 继承一个 viewController。另一方面,如果我们想在我们的 viewController 中有一个更复杂的布局(比如 tableView 只占用 viewController 的一部分,而其余空间被其他 UI 元素占用),那么我们可能想要viewController 继承自 UIViewController,然后在其中创建一个 tableView(就像我向您展示的第一个示例一样)。

Storyboard

当使用 Storyboard时,您应该确保做的第一件事是正确的,即您正在使用 AutoLayout 为您的 tableView 设置约束。在 main.storyboard 中,转到您要呈现的 View 并单击 tableView。然后,将鼠标悬停在“对齐”按钮上,就在这里:

enter image description here

确保选中“Horizo​​ntally in Container”和“Vertically in Container”。这会使您的 tableView 在其 superView 中居中。

然后,还要修改以下内容:

enter image description here

确保选中“限制边距”选项。这会将 tableView 的边距限制到它的 superView(理想情况下)。

关于ios - UiTableView 出现在 ViewController ios 上的错误位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53177540/

相关文章:

ios - 根据 "Face"元数据获取PHAssets

ios - 无法将类型 '[String]' 的值转换为预期的参数类型 'String' : while appending arrays to get data from Fireabse in table view cell

ios - Unity Prime31 照片提示在 iOS 10 和 XCode 8 上崩溃

ios - 使用比例约束会导致不同屏幕尺寸上的故障

xamarin - 如何在 iOS 16 中更改方向?

ios - UIActivityViewController - 上传 Youtube 视频

ios - Xamarin 中的 CGPointFromString

ios - 将 UIImage 转换为 8 位

ios - 如何检查 WKWebView URLAuthenticationChallenge 是否失败或成功?

objective-c - AVCaptureSessionPresetiPad 上的高分辨率?