最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

【整理】swift中自动布局中的约束条件中的left和leading,right和trailing的区别

Swift crifan 5969浏览 0评论

在折腾自动布局时,涉及到

left,right

leading,trailing

但是一直没搞清楚其中的区别

偶尔一次,通过实际效果,看出区别了:

代码:

            constrain(cell.textLabel!, cell.detailTextLabel!) { textLabel, detailTextLabel in
                textLabel.centerY == textLabel.superview!.centerY
                detailTextLabel.centerY == textLabel.centerY
                textLabel.leading == textLabel.superview!.leading + XPaddingAllPage
                detailTextLabel.leading >= textLabel.trailing + 55
                detailTextLabel.trailing == detailTextLabel.superview!.trailing XPaddingAllPage
            }

效果:

代码:

            constrain(cell.textLabel!, cell.detailTextLabel!) { textLabel, detailTextLabel in
                textLabel.centerY == textLabel.superview!.centerY
                detailTextLabel.centerY == textLabel.centerY
               
                textLabel.left == textLabel.superview!.left + XPaddingAllPage
                detailTextLabel.left >= textLabel.right + 55
                detailTextLabel.right == detailTextLabel.superview!.right XPaddingAllPage
            }

效果:

代码:

            constrain(cell.textLabel!, cell.detailTextLabel!) { textLabel, detailTextLabel in
                textLabel.centerY == textLabel.superview!.centerY
                detailTextLabel.centerY == textLabel.centerY
                
                textLabel.left == textLabel.superview!.left + XPaddingAllPage
                detailTextLabel.leading >= textLabel.trailing + 55
                detailTextLabel.trailing == detailTextLabel.superview!.trailing XPaddingAllPage
            }

效果:

代码:

            if indexPath.row == 0 {
                titleText += "名称"
                cell.textLabel?.text = titleText
               
                cell.detailTextLabel?.text = curGroupOrTopicItem.name
                cell.detailTextLabel?.font = detailTextFont
                cell.detailTextLabel?.textColor = detailTextColor
                cell.detailTextLabel?.numberOfLines = 0
                cell.detailTextLabel?.textAlignment = NSTextAlignment.Left
               
                //for debug
                cell.detailTextLabel?.backgroundColor = UIColor.purpleColor()
               
                constrain(cell.textLabel!, cell.detailTextLabel!) { textLabel, detailTextLabel in
                    textLabel.centerY == textLabel.superview!.centerY
                    detailTextLabel.centerY == textLabel.centerY
                   
                    textLabel.left == textLabel.superview!.left + XPaddingAllPage
                    detailTextLabel.leading >= textLabel.trailing + 55
                   
                    detailTextLabel.trailing == detailTextLabel.superview!.trailing XPaddingAllPage
                }
            } else if indexPath.row == 1 {
                titleText += "描述"
                cell.textLabel?.text = titleText

                let valueTextView = UITextView()
                valueTextView.text = curGroupOrTopicItem.intro
                valueTextView.font = detailTextFont
                valueTextView.textColor = detailTextColor
                valueTextView.selectable = false
                valueTextView.scrollEnabled = false
                valueTextView.textAlignment = NSTextAlignment.Left
                cell.contentView.addSubview(valueTextView)
               
                //for debug
                valueTextView.backgroundColor = UIColor.greenColor()
               
                constrain(cell.textLabel!, valueTextView) { textLabel, valueTextView in
                    textLabel.left == textLabel.superview!.left + XPaddingAllPage
                    textLabel.centerY == textLabel.superview!.centerY
                   
                    valueTextView.centerY == textLabel.centerY
                    valueTextView.leading >= textLabel.trailing + 55
                    valueTextView.trailing == valueTextView.superview!.trailing XPaddingAllPage
                }

            }

效果:

很明显:

下面行的内容,没有靠右对齐,并且左边也没和上面对齐

目前得出的结论:

好像left,right和leading,trailing,都是没区别的。

但是如果left,right(或leading,trailing)分别应用于label和textView后,却会导致不同的效果

-》看起来好像是label和textView对齐的性质不同而导致的,而不是自动约束的left,right和leading,trailing不同而导致的???

有待以后深究。

转载请注明:在路上 » 【整理】swift中自动布局中的约束条件中的left和leading,right和trailing的区别

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
91 queries in 0.182 seconds, using 22.33MB memory