- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我是新手,我编写了一个继承自 StackView
的 customView,并以编程方式创建了一个具有少量属性的 button
,当我将其添加到我的自定义 View 时,我有两个问题:
addArrangedSubview(myBtn)
,我的 View 会忽略那些我添加并填充了整个宽度。但是如果我使用 addSubView(myBtn)
,没关系(44x44 的蓝色方 block )addArrangedSubview(myBtn)
,addTarget()
不起作用并且myBtn
不可点击,但是当我使用 addSubView(myBtn)
时,它起作用了完美。这是我的自定义 View 类:
import UIKit
class RatingControl: UIStackView {
//MARK: Initialization
override init(frame: CGRect) {
super.init(frame: frame)
setupButtons()
}
required init(coder: NSCoder) {
super.init(coder:coder)
setupButtons()
}
//MARK: Private Methods
private func setupButtons() {
// Create the button
let button = UIButton()
button.backgroundColor = UIColor.blue
// Add constraints
button.translatesAutoresizingMaskIntoConstraints = false
button.heightAnchor.constraint(equalToConstant: 44.0).isActive = true
button.widthAnchor.constraint(equalToConstant: 44.0).isActive = true
// Setup the button action
button.addTarget(self, action: #selector(ratingButtonTapped(_:)), for: .touchUpInside)
// Add the button to the stack
addArrangedSubview(button)
}
//MARK: Button Action
@objc func ratingButtonTapped(_ sender: Any) {
print("Button pressed 👍")
}
}
这是预览:
addSubView()
和 addArrangedSubview()
有什么区别?为什么会出现这些问题?
最佳答案
我假设您想水平添加几个按钮(例如使用典型的“星级”评级控件)。
一个 UIStackView
,从它的 .addArrangedSubview()
方法可以猜到,安排它的 subview ,基于它的 .axis
、.alignment
、.distribution
和 .spacing
属性,以及它的 框架
。
因此,请阅读有关 UIStackView
的内容以及如何使用它。最有可能的是,您目前正在使用以下方式限制您的自定义 View :
因此,将您的按钮添加为 arrangedSubView
会导致它拉伸(stretch)到堆栈 View 的宽度,因为这是默认设置。
将其添加为 subview
只是将按钮覆盖在堆栈 View 上,而不是让堆栈 View 对其进行排列,然后堆栈 View 的高度可能为零——因此按钮无法点击。
尝试在添加自定义堆栈 View 时仅设置顶部和前导约束。这应该会为您提供一个可以点击的 44 x 44
按钮。
当您使用 .addArrangedSubview()
添加更多按钮时,这些按钮将水平排列,这可能正是您想要的。
关于ios - addArrangedSubview 与 addSubview,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55221703/
我已经用 swift 编写了一堆代码并且更喜欢以编程方式编写很多代码,我想知道这两者之间的区别是什么: self.view.addSubview(someNewView) view.addSubvie
我在 tableviewcell 中有按钮。[cell addSubview:button] 和 [cell.contentview addsubview:button] 有什么区别?因为当我在 ta
我知道Apple建议开发者在self.contentview中添加 subview ,但是如果我们在UITableViewCell中添加 subview 会发生什么? 最佳答案 ios 7以后[s
我需要在 viewcontroller 的顶部显示 loaderView let loaderView: UIView() self.view.window?.addSubview(loaderVie
// Create and add a sub view CGRect viewRect = CGRectMake(0, 0, 200, 200); UIView *a_sub_view = [[UI
如何将 View 添加到窗口,以便调整 View 大小以适合窗口框架? 问题 我正在制作一个包含 2 个 View 的工作表窗口,其中一次只能看到其中一个 View ,因此 View 与窗口的大小相同
当我使用 addSubview方法,然后 removeFromSubview加载下一个 ViewController 然后加载 View 几次后,我的应用程序崩溃。我有很多关于 View 的图像。 尽
我试图让我的标 checkout 现动画: - (void)handleSingleTap:(UIGestureRecognizer *)gestureRecognizer { if (isS
我有两个 UIView,名为 A 和 B,我将 UIView B 添加到 A 的 subview 中: [A.view addSubview B] 我想知道是否有任何委托(delegate)或 Hoo
我有类似的东西: (1) NavigationController --> (2) UITableViewController --> (3 afted clicked in a cell) UIVi
我有动画打开 NSViewController,它尝试添加 subview : [self.view addSubview:joinToCompanyView]; joinToCompanyView.
在使用 addSubview 时防止内存泄漏的正确方法是什么?我收到来自 Instruments 的投诉,说这段代码存在漏洞。我做错了什么? 示例代码: 我的.h @interface MyCusto
以下代码的结果是:我没有看到标签。 我的理解是标签的 intrinsicSize 会允许标签拉伸(stretch)。为什么没有发生这种情况? import UIKit class ViewContro
错误 -> “UIVIew”没有名为“addSubView”的成员 override func viewDidLoad() { super.viewDidLoad() // Do an
在使用 addSubview() 加载 View Controller View 时,我遇到了一个奇怪的行为: 硬件和软件:OSX 10.9.5 Mavericks、XCode 6 Beta 6 上的
使用 addSubview 方法向 UIAlertView 添加一些控件在 iOS7 中已弃用。据我所知,Apple promise 添加 contentView 属性。 iOS 7 现在发布了,我看
我有一个已加载到 MainWindow.xib 中的 View 。它只是一个带有 uiimageview 的 View ,在整个屏幕(320 X 480)上显示图像。当应用程序加载时,我显示此 Vie
我有一个基于 UITabBar 的应用程序,它运行得很好。在某些情况下,我会显示不同的 UIViewController。现在让我烦恼的是,我必须调整测试 Nib 的框架(并且仅测试 Nib !)才能
嗨,我正在 xcode 3.2.3 中编写一个应用程序。我想要做的就是切换到另一个 View ,但我不确定这样做的最佳方法。我可以通过这两种方式中的任何一种... PreferencesViewCon
我遇到了一个奇怪的问题..我想向我当前的 View 添加一个 UIViewcontroller(称为 iView)。我通过调用 iView = [[KFKaraokeInfosView allo
我是一名优秀的程序员,十分优秀!