【问题标题】:Can card be restricted to width of view?卡片可以限制视图宽度吗?
【发布时间】:2016-02-12 05:10:10
【问题描述】:

我正在尝试显示一张卡片,但当detailTextLabel 变得太长时,它会导致卡片超出视图。我不确定这是否是一个错误。我已将.numberOfLines 调整为各种数字,但似乎没有效果。

    // Detail label.
    let detailLabel: UILabel = UILabel()
    detailLabel.text = "When this text gets too long it does not wrap, it will extend off the page"
    detailLabel.font = UIFont(name: "Roboto-Thin", size: 18)
    detailLabel.numberOfLines = 0
    cardView.detailLabel = detailLabel

编辑

这是 cardView 的完整代码。

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    let cardView: CardView = CardView()

    // Title label.
    let titleLabel: UILabel = UILabel()
    titleLabel.text = self.type[indexPath.row].capitalizedString
    titleLabel.textColor = MaterialColor.blue.darken1
    titleLabel.font = UIFont(name: "Roboto-Medium", size: 23)
    cardView.titleLabel = titleLabel

    // Detail label.
    let detailLabel: UILabel = UILabel()
    detailLabel.text = "When this text gets too long it does not wrap, it will extend off the page"
    detailLabel.font = UIFont(name: "Roboto-Thin", size: 18)
    detailLabel.numberOfLines = 100
    cardView.detailLabel = detailLabel

    // Yes button.
    let btn1: FlatButton = FlatButton()
    btn1.pulseColor = MaterialColor.blue.lighten1
    btn1.pulseScale = false
    btn1.setTitle("Ok", forState: .Normal)
    btn1.setTitleColor(MaterialColor.blue.darken1, forState: .Normal)


    // Add buttons to left side.
    cardView.leftButtons = [btn1]

    // To support orientation changes, use MaterialLayout.
    view.addSubview(cardView)
    cardView.translatesAutoresizingMaskIntoConstraints = false
    MaterialLayout.alignFromTop(view, child: cardView, top: self.view.frame.height / 4)
    MaterialLayout.alignToParentHorizontally(view, child: cardView, left: 10, right: 10)
}

}

【问题讨论】:

  • 你在使用自动布局吗?您还可以动态地将卡片添加为子视图吗?
  • 是的,使用自动布局。我已经在编辑中包含了整个代码。它几乎是从 github repo 复制粘贴。

标签: swift cosmicmind


【解决方案1】:

所以问题是您使用的是 UITableViewController。当您将 CardView 添加到您的视图属性时,它实际上是将其添加到 UITableView,这将导致它在您滚动 tableView 时滚动。它没有正确扩展的另一个问题是由于“|” AutoLayout 中的值,当放置在 TableView 中时似乎会中断。这可能是 TableView 在数学上设置的结果。

无论哪种方式,为避免滚动问题,您都需要使用 UIViewController 并将 UITableView 作为子视图添加到其中,或者如果您热衷于使用 UITableViewController,则应将其作为子 ViewController 添加到父 ViewController,您也将添加您的 CardView。这最终也将解决您的边界问题。

在 Material repo -> Examples/Programmatic/SideViewController 示例项目中,您可以找到一个添加为子视图的 tableView,在那里,您可以像在当前项目中那样放置 CardView 代码.

【讨论】:

    【解决方案2】:

    我在示例项目中尝试了您的代码 sn-p 并且似乎一切正常。我用了这段代码

    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        let cardView: CardView = CardView()
    
        // Title label.
        let titleLabel: UILabel = UILabel()
        titleLabel.text = "Activities dhajshd ajshd ashd kjashd jkasd jk asjkd asjk d"
        titleLabel.textColor = MaterialColor.blue.darken1
        titleLabel.font = UIFont(name: "Roboto-Medium", size: 23)
        cardView.titleLabel = titleLabel
    
        // Detail label.
        let detailLabel: UILabel = UILabel()
        detailLabel.text = "When this text gets too long it does not wrap, it will extend off the page"
        detailLabel.font = UIFont(name: "Roboto-Thin", size: 18)
        detailLabel.numberOfLines = 100
        cardView.detailLabel = detailLabel
    
        // Yes button.
        let btn1: FlatButton = FlatButton()
        btn1.pulseColor = MaterialColor.blue.lighten1
        btn1.pulseScale = false
        btn1.setTitle("Ok", forState: .Normal)
        btn1.setTitleColor(MaterialColor.blue.darken1, forState: .Normal)
    
    
        // Add buttons to left side.
        cardView.leftButtons = [btn1]
    
        // To support orientation changes, use MaterialLayout.
        view.addSubview(cardView)
        cardView.translatesAutoresizingMaskIntoConstraints = false
        MaterialLayout.alignFromTop(view, child: cardView, top: self.view.frame.height / 4)
        MaterialLayout.alignToParentHorizontally(view, child: cardView, left: 10, right: 10)
    }
    

    标题和详细信息标签对于单行来说都太长,并且 CardView 会做出适当的响应。

    所以我怀疑您的“观点”可能过于宽泛。你能确认你的 ViewController 的“宽度”最多是设备的宽度吗?

    【讨论】:

    • 通过print(self.view.frame.width) 告诉我我的视野是 320px 宽,这对于 5S 模拟器是正确的。
    • 您是否在设备上发现了此问题?还是只是 5s 模拟器?
    • 刚刚在物理设备上尝试过,同样的问题出现了。同样,不确定这是否相关,但如果我在 TableView 中向下滚动,卡片会显示在顶部(看不见)。
    • 滚动问题让我相信存在层次结构问题。好的,你能回答这些问题,我看看能不能复现。
    • 设备操作系统?设备型号(5s、6s 等)?材料版本?
    猜你喜欢
    • 2020-09-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-15
    相关资源
    最近更新 更多