- (IBAction)btnA:(id)sender {
if(self.height.constant == 300) {
self.height.constant = 50;
} else {
self.height.constant = 300;
}
[self.subView1 setNeedsUpdateConstraints];
[UIView animateWithDuration:1.0 animations:^{
[self.subView1 layoutIfNeeded];
}];
}
Run Code Online (Sandbox Code Playgroud)
我正在使用这个代码,但iOS10它没有动画它只是跳跃增加和减少mySubview1高度.为什么?
相同的代码工作正常 iOS9
下面我列出了一些代码供您结账.我正在尝试自定义UIView并添加另一个自定义子视图.这个子视图应该以这样的方式约束到父视图,它基本上只是在相同的维度上放置,而父级只是作为包装器.
NSLayoutConstraint到目前为止,我已尝试使用和失败的悲惨.视图从未真正显示出来.我有一个left,right,bottom和top约束,它应与父视图对齐.
第一个问题是,有人请在使用以下方法时解释或纠正我的逻辑.我想到的项目参数是你想为(customViewChild)设置约束的实际视图.该属性是说我希望my的左边缘customViewChild用于此约束.在relatedBy看起来很简单的,虽然我可能是错的,最后的toItem指向自我这是我CustomViewParent这也有一个.left属性地说,我希望我的孩子和家长的左边缘对齐.这个逻辑有缺陷还是我做错了什么?
NSLayoutConstraint(item: customViewChild!,
attribute: .left,
relatedBy: .equal,
toItem: self,
attribute: .left,
multiplier: 1.0,
constant: 0.0)
Run Code Online (Sandbox Code Playgroud)
我知道下面的例子很容易用IB完成,但我想了解NSLayoutConstraint,所以请提供相关答案.最后,如果有人能够真正纠正这个代码,那么我有一个有效的例子,那将是非常棒的.
class CustomViewParent: UIView {
var customViewChild: UIView?
override init(frame: CGRect) {
super.init(frame: frame)
setConstraints()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
setConstraints()
}
func setConstraints() {
customViewChild = UIView()
addSubview(customViewChild!)
customViewChild?.translatesAutoresizingMaskIntoConstraints = false
let leftConstraint = NSLayoutConstraint(item: customViewChild!,
attribute: .left,
relatedBy: .equal,
toItem: self, …Run Code Online (Sandbox Code Playgroud) 我正在将 UITableView 添加到垂直 UIStackView 中。该垂直 UIStackView 位于 UIScrollView 内。
但是,除非我对其施加显式高度约束,否则该表不会显示,而我显然不想这样做。
根据这个SO问答UITableView not shown inside UIStackView这是因为“stackview尝试尽可能地压缩内容”
如果我将 UILabel 添加到 StackView 中,它会显示正常。UITableView 有一些特定的东西意味着它不是。我正在使用 Xamarin 并在代码中创建 UITableView
this.recentlyOpenedPatientsTable = new UITableView()
{
RowHeight = UITableView.AutomaticDimension,
EstimatedRowHeight = 44.0f,
AllowsMultipleSelectionDuringEditing = false,
TranslatesAutoresizingMaskIntoConstraints = false,
Editing = false,
BackgroundColor = UIColor.Clear,
TableFooterView = new UIView(),
ScrollEnabled = false,
};
Run Code Online (Sandbox Code Playgroud)
UIScrollView 固定在视图的顶部、底部、左侧和右侧并且工作正常。它需要我期望的高度。
我已经尝试过这个问题中的两个建议,但都没有奏效。我觉得很奇怪,我找不到其他人有这个问题。
还有其他建议吗?
我有两种看法:toastView和view。toastView是 的子视图view。我想toastView在 y 轴上定位view高度的 80%。如何在代码中使用常量来做到这一点?
我假设有一个类似的方法:
[toastView.topAnchor constraintEqualToAnchor:view.heightAnchor multiplier:0.8].active = YES;
Run Code Online (Sandbox Code Playgroud)
但我不能混合NSLayoutDimension(宽度和高度)和NSLayoutYAxisAnchor(X 和 Y)
这就是它在设计中的样子:

我UILabel在自定义表格单元格中有两个s,每个都在Interface Builder中将"Lines"设置为"0".它们垂直堆叠,左右边缘对齐,在一个表格内,行高由自动布局决定.但其中一人坚持要求截断.为什么?它应该占用所需的线并推出表格单元的高度.

我有以下代码,只需AutoLayout在我ViewController的编程中使用约束来简单地将红色正方形居中view:
class ViewController: UIViewController {
let square: UIView
required init?(coder aDecoder: NSCoder) {
let squareFrame = CGRectMake(0.0, 0.0, 500.0, 500.0)
self.square = UIView(frame: squareFrame)
super.init(coder: aDecoder)
}
override func viewDidLoad() {
self.view.addSubview(self.square)
self.square.backgroundColor = UIColor.redColor()
self.view.backgroundColor = UIColor.blueColor()
print(self.square)
setupConstraints()
print(self.square)
}
func setupConstraints() {
self.square.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint(item: self.view, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal,
toItem: self.square, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant:0).active = true
NSLayoutConstraint(item: self.view, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal,
toItem: self.square, attribute: NSLayoutAttribute.CenterY, multiplier: …Run Code Online (Sandbox Code Playgroud) 我想显示两个 UILabel,但是它们UILabel的文本长度是可变的。根据textsize UIlabel的宽度需要使用自动布局来增加。
为此,我在下面为这两个UIlabel的自动布局
第一个标签:
1)leading Space
2)Top space
3)Width
4)height
5)Horizontal spacing
Run Code Online (Sandbox Code Playgroud)
第二标签:
1)Trailing space
2)Top space
3)Width
4)height
Run Code Online (Sandbox Code Playgroud)
我们应该怎么做 ?
请帮我。
textLabel1.numberOfLines = 0
textLabel1 .sizeToFit()
textLabel1.text = "asdfdsfdghjgjhkhkjlhjkhjk"
textLabel2.numberOfLines = 0
textLabel2 .sizeToFit()
textLabel2.text = "asdfdsfdghjgjhkhkjlhjkhjk"
Run Code Online (Sandbox Code Playgroud) 我有三个相同大小的按钮查看.每个按钮占据视图的1/3部分.
喜欢这个图片:
如果我删除/隐藏一个按钮,则两个按钮宽度应该相等地增加并占据视图的1/2部分.如果我删除两个按钮,那么一个按钮大小应该是相同的视图大小.
我的问题是,如何使用Autolayout.
我使用可视化格式在单元格中布局子视图,如下所示:
contentView.addSubview(accountLabel)
contentView.addSubview(contentLabel)
contentView.addSubview(timeLabel)
let views: [String : Any] = [
"accountLabel": accountLabel,
"contentLabel": contentLabel,
"timeLabel": timeLabel
]
let hConstraint1 = NSLayoutConstraint.constraints(withVisualFormat: "H:|-15-[accountLabel]-[timeLabel]-8-|", options: [.alignAllCenterY], metrics: nil, views: views)
let hConstraint2 = NSLayoutConstraint.constraints(withVisualFormat: "H:[contentLabel]-8-|", options: [], metrics: nil, views: views)
let vConstraint = NSLayoutConstraint.constraints(withVisualFormat: "V:|-12-[accountLabel]-8-[contentLabel]-12-|", options: [.alignAllLeading], metrics: nil, views: views)
NSLayoutConstraint.activate(hConstraint1+hConstraint2+vConstraint)
Run Code Online (Sandbox Code Playgroud)
在模拟器中运行时它看起来不错:
但是,调试视图层次结构时会出现一些布局问题:
为什么在运行时UI看起来正常时会出现这些布局问题?
上面的可视格式字符串有问题吗?或者是Xcode的错误?
如何删除这些布局问题?
ios-autolayout ×10
ios ×9
autolayout ×6
swift ×3
objective-c ×2
uilabel ×2
uitableview ×2
cocoa ×1
debugging ×1
iphone ×1
uiview ×1