gpt4 book ai didi

ios - 我怎样才能把它从 swift 变成 objective-c?

转载 作者:行者123 更新时间:2023-11-28 09:47:08 27 4
gpt4 key购买 nike

我正在学习教程,但速度很快,而且我正在处理的项目部分是用 Objective-C 编写的。我已经“翻译”了一半的代码,但我还不能更改这部分。

// Constrain the top of the button bar to the bottom of the segmented control
buttonBar.topAnchor.constraint(equalTo: segmentedControl.bottomAnchor).isActive = true
buttonBar.heightAnchor.constraint(equalToConstant: 5).isActive = true
// Constrain the button bar to the left side of the segmented control
buttonBar.leftAnchor.constraint(equalTo: segmentedControl.leftAnchor).isActive = true
// Constrain the button bar to the width of the segmented control divided by the number of segments
buttonBar.widthAnchor.constraint(equalTo: segmentedControl.widthAnchor, multiplier: 1 / CGFloat(segmentedControl.numberOfSegments)).isActive = true

我已经有了这个,但我不知道如何放置 .isActive 部分。

[buttonBar.topAnchor constraintEqual:_segmentedControl.bottomAnchor];

我该怎么做?

最佳答案

你应该可以做到

[buttonBar.topAnchor constraintEqualToAnchor:_segmentedControl.bottomAnchor].active = YES;

Apple 文档通常是一个很好的起点:

https://developer.apple.com/documentation/uikit/nslayoutanchor?language=objc

作为对您关于乘数的评论的回应,请再次阅读文档。例如向下滚动我们看到的:

Note:

You never use the NSLayoutAnchor class directly. Instead, use one of its subclasses, based on the type of constraint you wish to create.
- Use NSLayoutXAxisAnchor to create horizontal constraints.
- Use NSLayoutYAxisAnchor to create vertical constraints.
- Use NSLayoutDimension to create constraints that affect the view’s height or width.

所以宽度和高度约束实际上是 NSLayoutDimension 并且那些有 extra methods ,特别是:constraintEqualToAnchor:multiplier:

即使您不想每次都引用文档,自动完成也是您的好 helper 。只需开始输入类似 [self.widthAnchor constraint 的内容,看看会出现什么

关于ios - 我怎样才能把它从 swift 变成 objective-c?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58190377/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com