- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我想要的:
我想以水平方式并排放置 2 个 Views
。为此,我正在以编程方式创建 UIStackView
。看下面的片段
我做了什么:
let mStackView = UIStackView()
mStackView.axis = UILayoutConstraintAxis.horizontal
mStackView.distribution = UIStackViewDistribution.fillEqually
mStackView.alignment = UIStackViewAlignment.center
mStackView.spacing = 10
现在我想在其中放置 2 个 UIView
。每个 UIView
都会使 UIImage
和 UILabel
居中对齐。为此,我在每个 UIView
UIStackView
看看这个片段。
let mDonationView = UIView()
mDonationView.backgroundColor = UIColor.green
let SV = UIStackView()
SV.axis = UILayoutConstraintAxis.vertical
SV.distribution = UIStackViewDistribution.fill
SV.alignment = UIStackViewAlignment.leading
SV.spacing = 1
SV.alignment = .top
let imageName = "Donate"
let image = UIImage(named: imageName)
let imageView = UIImageView(image: image!)
imageView.frame = CGRect(x: 0, y: 0, width: 100, height: 200)
let lbl = UILabel(frame: CGRect(x: 0, y: 0, width: 150, height: 21))
lbl.text = "Donate"
lbl.textColor = UIColor(hex:AppColor.colorTextPrimary)
lbl.font = lbl.font.withSize(14)
SV.addArrangedSubview(imageView)
SV.addArrangedSubview(lbl)
SV.translatesAutoresizingMaskIntoConstraints = false
mDonationView.addSubview(SV)
最后,我将其添加回主 UIStackView
,即第一个代码片段中的 mStackView
。见下文:
mStackView.addArrangedSubview(mDonationView)
类似地,我正在创建另一个 View 并以上面显示的方式添加。
问题:
UIImageView
和 UILabel
在左侧对齐,我希望水平居中,垂直居中UIView
.UIView
的 width
必须相等 UIView
没有使用colors
我已经给每个 UIView
最佳答案
几个笔记...
首先,您最好使用自动布局和约束(而不是显式框架),尤其是堆栈 View 。
其次,UIStackView
属性可能与您的想法略有不同...
对于水平堆栈 View ,
alignment
控制排列的 subview 的垂直对齐方式distribution
控制 subview 如何水平地对于垂直堆栈 View ,
alignment
控制排列的 subview 的水平对齐方式distribution
控制 subview 如何垂直我假设这就是您想要的:
这是用于创建它的代码(很多评论,应该让你上路):
class SampleViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let mStackView = UIStackView()
// horizontal stack view
mStackView.axis = .horizontal
// distribution = fillEqually ... means make each arranged subview equal width
mStackView.distribution = .fillEqually
// alignment = center ... means center the arranged subviews vertically
mStackView.alignment = .center
// spacing = 10 ... horizontal gap between arranged subviews
mStackView.spacing = 10
// create the left-side "Donate" view
let donateView = createMyView("Donate", bgColor: UIColor.green, txtColor: UIColor.black)
// create the right-side "Receive" view
let receiveView = createMyView("Receive", bgColor: UIColor.yellow, txtColor: UIColor.blue)
mStackView.addArrangedSubview(donateView)
mStackView.addArrangedSubview(receiveView)
// using auto-layout
mStackView.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(mStackView)
// setup constraints for mStackView
// we'll make it the width of the view, and centered vertically
// allow the height of the donate and receive views to determine the height of the stack view, so
// no bottom or height constraint
NSLayoutConstraint.activate([
mStackView.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor, constant: 0.0),
mStackView.trailingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.trailingAnchor, constant: 0.0),
mStackView.centerYAnchor.constraint(equalTo: view.centerYAnchor, constant: 0.0),
])
}
func createMyView(_ imageName: String, bgColor: UIColor, txtColor: UIColor) -> UIView {
let myView = UIView()
myView.backgroundColor = bgColor
let vStackView = UIStackView()
// vertical stack view
vStackView.axis = .vertical
// alignment = center ... means the arranged subviews will be centered horizontally
vStackView.alignment = .center
// distribution = fill ... means the arranged subviews will fill the height of the stack view
vStackView.distribution = .fill
// spacing = 1 ... vertical gap between arranged subviews
vStackView.spacing = 1
let vImageName = imageName
let vImageView = UIImageView()
if let vImage = UIImage(named: vImageName) {
vImageView.image = vImage
}
let vLabel = UILabel()
vLabel.text = imageName
vLabel.textColor = txtColor
vLabel.textAlignment = .center
vLabel.font = vLabel.font.withSize(14)
vLabel.backgroundColor = UIColor(white: 0.9, alpha: 1.0)
// add the stack view to myView
myView.addSubview(vStackView)
// add the image view and label as arranged subviews of the stack view
vStackView.addArrangedSubview(vImageView)
vStackView.addArrangedSubview(vLabel)
// we're going to use auto-layout
myView.translatesAutoresizingMaskIntoConstraints = false
vStackView.translatesAutoresizingMaskIntoConstraints = false
vImageView.translatesAutoresizingMaskIntoConstraints = false
vLabel.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
// add width and height constraints for the image view
vImageView.widthAnchor.constraint(equalToConstant: 100.0),
vImageView.heightAnchor.constraint(equalToConstant: 200.0),
// constrain the stack view to all four side of myView
vStackView.topAnchor.constraint(equalTo: myView.topAnchor, constant: 0.0),
vStackView.bottomAnchor.constraint(equalTo: myView.bottomAnchor, constant: 0.0),
vStackView.leadingAnchor.constraint(equalTo: myView.leadingAnchor, constant: 0.0),
vStackView.trailingAnchor.constraint(equalTo: myView.trailingAnchor, constant: 0.0),
])
return myView
}
}
关于ios - 创建一个内部有 2 个 UiView 的水平 Stackview,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54865202/
我有一个带有 rowHeight 属性设置为 UITableViewAutomaticDimension 的 TableView 的 View Controller : class ViewContr
我在 IB 中创建了一个 Stackview,它具有垂直方向。此堆栈 View 与父 View 的宽度相等。 例如,现在我以编程方式创建了一个 Stackview let stackViewHoriz
我有一个 Storyboard,其中有一个用于 UIView 的单独 View Controller 。在 UIView 中,我必须在单行中加载 10 个按钮,而在 iPad 中,我必须在两行中加载
我有一个看起来像这样的布局。 View Controller 嵌入在导航 Controller 中,而导航 Controller 又嵌入在选项卡栏 Controller 中,因此 View 同时具有顶
在 UITableViewCell 中,我创建了一个垂直的 stackView。 我在垂直堆栈 View (由一个 UILabel(A) 和 UIImageView 组成)和一个 UILabel(B)
我使用 Xcode 10/Swift 5/iOS 12 并得到了这个布局: 红色(垂直)StackView 设置为: 分布:“平均填充” 高度:300(固定) 框架:10(每边) 间距:10 5 个水
我正在研究实现自定义控件 iOS 教程(我会链接它,但不允许使用两个以上的链接)。 我现在在 StackView 中生成了 5 个按钮。我为每个按钮实现了这些约束: button.translates
我正在尝试实现 UIView这将是一个标题。 备注 我只专注于在这个问题中实现标题(如下所示)。 这是它的外观(标题为黄色) 基本上,标题 UIView应该有 UIBUTTON一直到左边和一个UILa
我有一个 UIStackView这是根据其宽度改变轴。它包括两个UView仅限s。有一个非常简单的设置(可以复制/粘贴到默认的 Xcode 项目): class ViewController: UIV
我将三个垂直堆栈 View 放到一个水平堆栈 View 中。 虽然水平堆栈 View 的分布设置为等间距,但第二个垂直堆栈 View 在模拟器上左对齐(第一个左对齐,第三个右对齐)。 会不会有什么原因
是否可以在设备处于纵向模式时使用垂直堆栈 View ,但在设备处于横向模式时切换到水平堆栈 View ,以充分利用空间。 最佳答案 单击属性检查器中的小加号按钮添加一个变体,在此屏幕截图中我已经添加了
我有一个包含堆栈 View 的 ViewController。在此堆栈 View 中还有 3 个堆栈 View 。第一个包含 3 个标签,第二个包含更多带有按钮和标签的堆栈 View ,第三个包含 2
请告诉我我是否选择了正确的方法或者还有其他选择吗? 我从服务器收到一组对象。 根据任务,我必须将数组的每个对象放置在表格单元格中 我不知道会有多少对象,我只是说它们都有相同的结构,并且应该自动放置。
我在垂直 StackView 中有一个Horizontal StackView。 水平 StackView 包含图像(w=30,h=30)和标签。 需要 我需要根据标签固有内容的大小使图像和标签水
我有一个堆栈 View ,通过更改间距来折叠和展开堆栈 View ,将其用作下拉菜单。 堆栈 View 内有相同大小的按钮。当间距为负时,它们会出现在彼此的顶部。我设置图层 zIndex 来确定要在顶
我正在 Stackview 上工作并添加两个标签,我想调整标签的位置,使得下面的标签应该有点右侧。例如,在 UITableViewCell 中,我们有一个标题和第二个描述,随着描述部分内容的增加,一直
在自定义表格单元格中,我尝试在单击按钮时隐藏/显示 View 。我能够实现这一点,但无法删除空白。如何删除这些空格。我对堆栈 View 使用了自动约束,但似乎没有用。您能建议一种简单的方法来在 Vie
我有一个带有 UIStackView 的表格 View 单元。在我的 cellForRow 方法中,我根据某些因素将 stackview 的轴从水平更改为垂直。 由于某种原因,当我将包含表格单元格的
我想用不同的对齐方式对齐 3 个堆栈 1-Image 0-0 两边如下图 2- 用户信息 + 文字 10-10 形成两边 问题是当我将它们堆叠在一起时,我只能将它们全部设置为 0 到所有边距,每堆没有
我正在尝试在 TableView 的单元格方法中使用新的 StackView 来满足某些要求 一些背景 - 我有一个 TableView,它的单元格中有一个 stackView。 stackView
我是一名优秀的程序员,十分优秀!