标签: nslayoutanchor

在iOS上,边距,边缘插入,内容插入,对齐方式,布局边距,锚点​​之间有什么区别......?

似乎有几个不同的选项/术语,iOS社区中的人在布局方面使用(例如,UIEdgeInsets是一种类型,但有时我会听到/读取"设置插图"或布局边距与布局指南).

我总能找到一个有效的选项.但我不确定我是否正在使用合适的工具.

有人可以帮助在布局的这些不同方面之间提供一些清晰度,以及何时以最佳方式使用每个方面?

layout view ios autolayout nslayoutanchor

26
推荐指数
3
解决办法
4276
查看次数

在扩展NSLayoutAnchor中编译错误

我试图在NSLayoutAnchor上创建一个扩展方法来接受一个乘数.我得到一个奇怪的Swift编译错误(Swift 3,XCode 8.2.1)不在扩展文件中,而是在相应的.h文件中(该文件由编译器自动生成)用于模块:

@interface NSLayoutAnchor (SWIFT_EXTENSION(MYMODULENAME))
- (NSLayoutConstraint * _Nonnull)constraintTo:(NSLayoutAnchor</* AnchorType */> * _Nonnull)anchor multiplier:(CGFloat)m;
@end
Run Code Online (Sandbox Code Playgroud)

错误指向/* AnchorType */并说:"预期类型".嗯,这是有道理的,因为/* AnchorType */它不是一种类型,而是一种评论.但是我AnchorType在方法签名中使用了type参数.

扩展源代码如下:

extension NSLayoutAnchor {
  open func constraint(to anchor: NSLayoutAnchor<AnchorType>, multiplier m: CGFloat) -> NSLayoutConstraint {
  // ...
  }
}
Run Code Online (Sandbox Code Playgroud)

uikit swift nslayoutanchor

2
推荐指数
1
解决办法
500
查看次数

编程约束 - 使用布局锚覆盖整个超级视图

是否可以使用布局锚来覆盖整个超级视图的屏幕?

使用 NSLayoutConstraints,我可以使用下面的代码实现这种效果

dimmingView = UIView()
dimmingView.translatesAutoresizingMaskIntoConstraints = false
dimmingView.backgroundColor = UIColor(white: 0.0, alpha: 0.5)

containerView?.addSubview(dimmingView)

NSLayoutConstraint(item: containerView!, attribute: .leadingMargin, relatedBy: .equal, toItem: dimmingView, attribute: .leadingMargin, multiplier: 1, constant: 0).isActive = true
NSLayoutConstraint(item: containerView!, attribute: .trailingMargin, relatedBy: .equal, toItem: dimmingView, attribute: .trailingMargin, multiplier: 1, constant: 0).isActive = true
NSLayoutConstraint(item: containerView!, attribute: .topMargin, relatedBy: .equal, toItem: dimmingView, attribute: .topMargin, multiplier: 1, constant: 0).isActive = true
NSLayoutConstraint(item: containerView!, attribute: .bottomMargin, relatedBy: .equal, toItem: dimmingView, attribute: .bottomMargin, multiplier: 1, constant: 0).isActive = true …
Run Code Online (Sandbox Code Playgroud)

ios nslayoutconstraint swift nslayoutanchor

1
推荐指数
1
解决办法
2476
查看次数

如何设置NSLayoutAnchor以生成纵横比为1:1的居中子视图

这听起来并不太难,但经过几个小时的尝试后,我无法得到合适的解决方案.

问题:我想设置约束(使用类NSLayoutAnchor)以获得以超视图为中心的子视图(colorImageView),纵横比为1:1并且总是处于有时具有宽度>高度的超视图中的最大尺寸或高度>宽度.

示例1:SuperViewSize = width:80,height = 100 - > subview应获得80的高度和宽度,并以(40/50)为中心

示例2:SuperviewSize = width:60,height = 50 - >子视图的高度和宽度应为50并且以(30/25)为中心

我不想/并且它没有解决问题/硬编码widthConstraint = min(superviewWidth,superviewHeight)因为在创建子视图的那一刻,超视图边界没有最终定义 - >所以我需要一个约束.

这是我到目前为止的尝试(仍然缺少在不同的超视图中最大可能设置高度和宽度的约束:

// CREATE CONSTRAINTS(center in SuperView)
colorImageView.centerXAnchor.constraint(equalTo: self.centerXAnchor).isActive = true
colorImageView.centerYAnchor.constraint(equalTo: self.centerYAnchor).isActive = true

// CREATE CONSTRAINTS (ratio 1:1)
colorImageView.widthAnchor.constraint(equalTo: colorImageView.heightAnchor, multiplier: 1.0).isActive = true
Run Code Online (Sandbox Code Playgroud)

我真的很感激一个暗示,怎么做.提前致谢 :))

autolayout nslayoutanchor

1
推荐指数
1
解决办法
2538
查看次数

如果设置了锚点,如何获取视图的高度

我目前正在尝试访问先前为其设置锚点的视图的高度。因此,例如,我searchTable在ViewController 中使用以下命令设置了左,右,顶部和底部锚点:

searchTable.topAnchor.constraint(equalTo: view.topAnchor).isActive = true
searchTable.leftAnchor.constraint(equalTo: view.leftAnchor).isActive = true
searchTable.widthAnchor.constraint(equalTo: view.widthAnchor).isActive = true
searchTable.bottomAnchor.constraint(equalTo: menuBar.topAnchor).isActive = true
Run Code Online (Sandbox Code Playgroud)

searchTable是我创建的类的对象,该类继承自UIView并具有UIImageView。我通过在init函数中使用以下内容来约束UIImageView:

imageView.topAnchor.constraint(equalTo: topContainer.topAnchor, constant: 10).isActive = true
imageView.leftAnchor.constraint(equalTo: topContainer.leftAnchor, constant: 10).isActive = true
imageView.heightAnchor.constraint(equalTo: self.heightAnchor, multiplier: topContainerMultiplier * imageProfileMultiplier).isActive = true
imageView.widthAnchor.constraint(equalTo: self.heightAnchor, multiplier: topContainerMultiplier * imageProfileMultiplier).isActive = true
Run Code Online (Sandbox Code Playgroud)

哪里:

let topContainerMultipler: Double = 1 / 7
let imageProfileMultipler: Double = 1 / 5
Run Code Online (Sandbox Code Playgroud)

在的init函数中searchTable,我试图将角半径设置为图像大小的一半。我试图用self.frame.heightself.frame.size.heightself.bounds.height并且还获得.constant了价值self.heightAnchor约束,但都返回0。

我认为有解决此问题的解决方案,但我一直找不到。

ios swift nslayoutanchor

1
推荐指数
1
解决办法
4293
查看次数

layoutMarginsGuide似乎有点过分吗?

当我尝试创建布局约束时,我了解了NSLayoutAnchor类。他们说:

注意

UIView不提供布局边距属性的锚点属性。相反,layoutMarginsGuide属性提供了一个UILayoutGuide对象,该对象表示这些边距。使用指南的锚点属性创建约束

好。但是同时我创建了没有UILayoutGuide属性的布局边距属性的锚属性。

     let inputsContainerView = UIView()
    inputsContainerView.backgroundColor = UIColor.white
    inputsContainerView.translatesAutoresizingMaskIntoConstraints = false
    view.addSubview(inputsContainerView)

    //need x,y,width,height constraints
    inputsContainerView.centerXAnchor.constraint(equalTo: view.centerXAnchor, constant: 0).isActive = true
    inputsContainerView.centerYAnchor.constraint(equalTo: view.centerYAnchor, constant: 0).isActive = true
    inputsContainerView.widthAnchor.constraint(equalTo: view.widthAnchor, constant: -24).isActive = true
    inputsContainerView.heightAnchor.constraint(equalToConstant: 150).isActive = true
Run Code Online (Sandbox Code Playgroud)

那么,为什么我们需要UILayoutGuide对象?事实证明,UIView是否为布局边距属性提供锚属性?请如果有人知道我会非常感激。

ios nslayoutconstraint swift nslayoutanchor

1
推荐指数
1
解决办法
1446
查看次数

heightAnchor.constraint 不会改变视图的高度

我使用这个代码:

func show(){
    view.translatesAutoresizingMaskIntoConstraints = false
    view.leftAnchor.constraint(equalTo: view.leftAnchor, constant: 0).isActive = true
    view.topAnchor.constraint(equalTo: view.topAnchor, constant: 0).isActive = true
    view.heightAnchor.constraint(equalToConstant: view.frame.height - 300).isActive = true
    view.rightAnchor.constraint(equalTo: view.rightAnchor, constant: 0).isActive = true
}

func hide(){
    view.translatesAutoresizingMaskIntoConstraints = false
    view.leftAnchor.constraint(equalTo: view.leftAnchor, constant: 0).isActive = true
    view.topAnchor.constraint(equalTo: view.topAnchor, constant: 0).isActive = true
    view.heightAnchor.constraint(equalToConstant: view.frame.height + 300).isActive = true
    view.rightAnchor.constraint(equalTo: view.rightAnchor, constant: 0).isActive = true
}
Run Code Online (Sandbox Code Playgroud)

showfunction 工作正常,但hidefunction 不起作用并且heightAnchor不起作用。

constraints autoresize swift ios-autolayout nslayoutanchor

1
推荐指数
1
解决办法
4189
查看次数

如何快速更新锚点约束

我想在 iOS 中创建一个类似 android 的菜单。我正在使用布局约束来设置约束。当我尝试在单击按钮时更新图像的左侧约束时遇到了这个问题(它应该动画到单击按钮的位置)。谁能帮我?它应该支持横向和纵向。

我不想使用第三方代码,也不想使用NSLayoutconstraint.

风景图片 肖像画

这是我的代码。

class MenuViewController: UIViewController {
            var buttons: [UIButton] = []
            var imageView : UIImageView!

            override func viewDidLoad() {
                super.viewDidLoad()
            }

            override func viewDidAppear(_ animated: Bool) {
                super.viewDidAppear(animated)
                addMenuItems()
            }

            func addMenuItems() {

                for _ in 0...4 {
                    let button : UIButton = UIButton()
                    self.view.addSubview(button)
                    button.backgroundColor = UIColor.darkGray
                    button.addTarget(self, action: #selector(didSelectedButton(_:)), for: .touchUpInside)
                    self.buttons.append(button)

                }

                for (index, button) in buttons.enumerated() {

                    button.setTitle(" \(index)", for: .normal)
                    if index …
Run Code Online (Sandbox Code Playgroud)

ios swift layout-anchor nslayoutanchor

0
推荐指数
1
解决办法
4296
查看次数