卡可以限制在视野范围内吗?

时间:2016-02-12 05:10:11

标签: swift cosmicmind

我正在尝试显示卡片,但当detailTextLabel太长时,会导致卡片超出视图范围。我不确定这是不是一个bug。我已将.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

Example of card extending beyond the page.

修改

以下是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)
}

}

2 个答案:

答案 0 :(得分:1)

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

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

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

答案 1 :(得分:0)

我在一个示例项目中尝试了您的代码片段,但似乎一切正常。我用过这段代码

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会做出适当的响应。

enter image description here

所以我怀疑你的观点"可能是广泛的。你能否确认你的宽度" ViewController的最大宽度是设备的宽度?