我正在制作一个使用 AR SceneKit View 的应用程序,但是当我限制在 iPhone 8 上的手机边缘时,它不会占据 iPhone X 上的整个屏幕。我认为这是因为安全区域和我已经玩了一个小时了,不知道如何解决这个问题。
这是我正在谈论的截图:https : //i.imgur.com/K1yBRaM.jpg
对不起,如果这是一个菜鸟问题,这是我第一次在 xcode 中工作
AUITableViewCell包含一个UIImageView以显示不同宽高比的图像。
表格视图单元格应根据图像大小比例调整其高度。所以图像视图宽度应该等于单元格宽度,图像高度是cell width * (image height / image width)将图像显示为aspectFit.
图像视图添加了自动布局约束:
heightConstraint = imageView.heightAnchor.constraint(equalTo: imageView.widthAnchor, multiplier: 1)
NSLayoutConstraint.activate([
imageView.leadingAnchor.constraint(equalTo: contentView.leadingAnchor),
imageView.trailingAnchor.constraint(equalTo: contentView.trailingAnchor),
imageView.topAnchor.constraint(equalTo: contentView.topAnchor),
imageView.bottomAnchor.constraint(equalTo: contentView.bottomAnchor),
heightConstraint
])
Run Code Online (Sandbox Code Playgroud)
当为单元格设置图像时,高度约束会更新:
func updateCell(withImage image: UIImage) {
// Update image
imageView.image = image
// Deactivate old height constraint
NSLayoutConstraint.deactivate([heightConstraint])
// Activate new height constraint
heightConstraint = imageView.heightAnchor.constraint(equalTo: imageView.widthAnchor, multiplier: image.size.height / image.size.width)
NSLayoutConstraint.activate([heightConstraint])
}
}
Run Code Online (Sandbox Code Playgroud)
设备上的单元格高度看起来是正确的,但它会记录自动布局错误:
[LayoutConstraints] Unable to simultaneously satisfy constraints.
Probably at least one …Run Code Online (Sandbox Code Playgroud) 这是我的视图层次结构
ScrollView
-> View
-> Stackview1
-> view1
-> view2
.
.
.
-> Stackview1
-> view1
-> view2
.
.
.
Run Code Online (Sandbox Code Playgroud)
所以这里我的 stackview 的高度是根据 .swift 文件中添加的视图动态生成的
如果我给超级视图提供底部约束,它只能使用单个堆栈视图,但如果我使用两个堆栈视图,则不会考虑第二个堆栈视图。
许多关于动画 AutoLayout 约束的教程建议更新约束的常量属性,然后调用layoutIfNeeded()动画块。
我的情况有点棘手。我有一个包含 3 个子视图的视图。这个 superview 的高度不是固定的 - 它被计算为其子视图的高度总和。在某些情况下,我要求这 3 个子视图之一切换其高度(它在 0 和 30 之间变化,即我想平滑地隐藏和显示它)。代码类似于:
// In superview
subview1.isVisibleInContext = !subview1.isVisibleInContext
// In subview
class MySubview: UIView {
@IBOutlet var heightConstraint: NSLayoutConstraint!
var isVisibleInContext = false {
didSet {
updateHeight()
}
}
func toggleHeight() {
let newHeight = isVisibleInContext ? 30 : 0
layoutIfNeeded()
heightConstraint.constant = newHeight
UIView.animate(withDuration: 0.8) {
self.layoutIfNeeded()
}
}
}
Run Code Online (Sandbox Code Playgroud)
不幸的是,这并不像我期望的那样工作。我可以看到我的子视图高度的平滑变化,但是在我为我的子视图高度约束设置新值后,我的超级视图的高度会立即重新计算。
我想看到超级视图的高度随着其子视图的增长或减少而逐渐增加/减少。
请有人指出我正确的方向。任何帮助表示赞赏。
非常感谢!
uiviewanimation ios autolayout nslayoutconstraint ios-autolayout
为什么要UIStackView调整子视图的大小?我认为它的工作时,我设置distribution一样.fill。但.equalSpacing并不暗示有这种行为。我是对的?
let stackView = UIStackView()
stackView.alignment = .firstBaseline
stackView.axis = .vertical
stackView.distribution = .equalSpacing
stackView.spacing = 0.0
stackView.translatesAutoresizingMaskIntoConstraints = true
self.view.addSubview(stackView)
stackView.snp.makeConstraints { (make) -> Void in
make.top.equalTo(button.snp.bottom)
make.left.equalTo(view).offset(16.0)
make.bottom.equalTo(view)
make.right.equalTo(view).offset(-16.0)
}
let exampleView = UIView(frame: .zero)
stackView.addArrangedSubview(exampleView)
exampleView.backgroundColor = .yellow
exampleView.snp.makeConstraints { (make) in
make.width.equalToSuperview()
make.height.equalTo(60.0)
}
Run Code Online (Sandbox Code Playgroud) 我在 Xcode 的故事板编辑器中做了很多自动布局,但我很少在代码中做任何事情。在一个特定的实例中,我需要创建与这些约束等效的编程:
这些约束被添加到textView超级视图是另一个名为commentBox. 到目前为止我已经尝试过这个,但是代码感觉是多余的,并导致冲突约束的自动布局错误:
//Trailing
textView.addConstraint(NSLayoutConstraint(item: textView, attribute: .trailing, relatedBy: .equal, toItem: cell.commentBox, attribute: .trailing, multiplier: 1, constant: 15))
//Leading
textView.addConstraint(NSLayoutConstraint(item: textView, attribute: .leading, relatedBy: .equal, toItem: cell.commentBox, attribute: .leading, multiplier: 1, constant: 15))
//Bottom
textView.addConstraint(NSLayoutConstraint(item: textView, attribute: .bottom, relatedBy: .equal, toItem: cell.commentBox, attribute: .bottom, multiplier: 1, constant: 10))
//Top
textView.addConstraint(NSLayoutConstraint(item: textView, attribute: .top, relatedBy: .equal, toItem: cell.commentBox, attribute: .top, multiplier: 1, constant: 10))
Run Code Online (Sandbox Code Playgroud)
知道我做错了什么吗?谢谢!
我的自定义标签栏:
class MyTabBar: UITabBar {
}
My custom tab Bar Controller:
class MyTabBarController: UITabBarController {
}
Run Code Online (Sandbox Code Playgroud)
我的问题是如何在我的自定义标签栏控制器中使用我的自定义标签栏?
我已经为按钮提供了尾随、顶部和高度约束。正如您在附加的屏幕截图中看到的那样,如果我删除所有给定的插图,那么按钮看起来很完美,并且按钮适合基于文本长度。
但我想给左右标题插入以及左图像插入。
然后按钮没有正确显示文本,如所附屏幕截图所示。
我发现了一些奇怪的东西。
我创建了一个自定义 UICollectionViewCell 并在其中创建了一个标签。
我只对标签的顶部、前导和底部应用了自动布局。
class TestCollectionViewCell: UICollectionViewCell {
@IBOutlet weak var label: UILabel!
override func awakeFromNib() {
super.awakeFromNib()
}
}
Run Code Online (Sandbox Code Playgroud)
如果文本很长,它会像下面一样从屏幕上剪下来。
如果文本太长,我希望文本仅显示为“...”。
所以我像这样应用了traling自动布局。
然后, collectionViewCell 的宽度被破坏了。
这是我的代码。
我已经设置了委托,数据源。
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
}
}
extension ViewController: UICollectionViewDataSource {
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 20
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
guard let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as? TestCollectionViewCell else { return …Run Code Online (Sandbox Code Playgroud) autolayout ×10
ios ×7
swift ×6
uistackview ×2
xcode ×2
aspect-ratio ×1
iphone ×1
snapkit ×1
swift4 ×1
uibutton ×1
uiimageview ×1
uikit ×1
uitableview ×1
width ×1