以编程方式添加内容后调整uiscrollview的大小

时间:2018-10-12 16:57:35

标签: ios uiscrollview

我在Uiscrollview中有一个带有uielements的Contentview。下面是我的故事板的屏幕截图: enter image description here

我想为该应用添加选项,如果用户单击该按钮,它将在contentview的底部添加标签(在红色带下划线的标签-dzialTerminOutlet下方)。 enter image description here

我正在使用以下代码以编程方式添加新标签:

let label = UILabel()
label.translatesAutoresizingMaskIntoConstraints = false

label.backgroundColor = UIColor.orange
label.textColor = UIColor.black
label.textAlignment = NSTextAlignment.center
label.text = "test label"
contentView.addSubview(label)


label.topAnchor.constraint(equalTo: dzialTerminOutlet.bottomAnchor, constant: 10).isActive = true
label.bottomAnchor.constraint(equalTo: contentView.bottomAnchor, constant: 85.0)
label.widthAnchor.constraint(equalToConstant: 200.0)
label.heightAnchor.constraint(equalToConstant: 10.0)

虽然滚动视图不会调整大小。这是什么问题?

3 个答案:

答案 0 :(得分:1)

1-您需要激活

#define vadds_raw v0
#define vadds vadds_raw##.16b
#define vrepquote v1.16b
#define vchrs0_raw v2
#define vchrs0 vchrs0_raw##.16b
#define stepmask_raw v7
#define stepmask stepmask_raw##.16b
#define halfmask_raw v8
#define adder_scratch v9

#define string x0
#define length x1
#define out x2
#define scratch_reg x3

.section    __TEXT,__text,regular,pure_instructions
.build_version ios, 12, 0
.globl    _aggregate
.p2align    2

_aggregate:
// load masks
movi vrepquote, #0x22

mov scratch_reg, 0x4080
movk scratch_reg, 0x1020, lsl 16
movk scratch_reg, 0x0408, lsl 32
movk scratch_reg, 0x0201, lsl 48
dup stepmask_raw.2d, scratch_reg

mov scratch_reg, 0xffff
movk scratch_reg, 0xffff, lsl 16
movk scratch_reg, 0xffff, lsl 32
movk scratch_reg, 0xffff, lsl 48

ins halfmask_raw.d[0], x31 // zero it out
ins halfmask_raw.d[1], scratch_reg

iter:
subs length, length, #16
ldur q2, [string]

cmeq vchrs0, vchrs0, vrepquote
and vchrs0, vchrs0, stepmask

movi.16b vadds_raw, #0
addv b0, vchrs0_raw.8b
and vchrs0, vchrs0, halfmask_raw.16b
addv b9, vchrs0
ins vadds_raw.b[1], adder_scratch.b[0]

str h0, [out]
add out, out, #2
add string, string, #16
b.hi iter
ret

2-您需要删除IB中在NSLayoutConstraint.activate([ label.topAnchor.constraint(equalTo: dzialTerminOutlet.bottomAnchor, constant: 10), label.bottomAnchor.constraint(equalTo: contentView.bottomAnchor, constant: 85.0), label.widthAnchor.constraint(equalToConstant: 200.0), label.heightAnchor.constraint(equalToConstant: 10.0) ]) dzialTerminOutlet之间建立的最低约束,以便能够插入新约束并相应地调整scrollView的大小,以避免它和

contentView

因此将其钩为插座,然后将其停用,或者在contentView中搜索底部约束并将其删除

答案 1 :(得分:0)

您是否将scrollView contentSize设置为新大小? scrollView.contentSize = CGSize(宽度:self.contentView.frame.size.width,高度:self.contentView.frame.size.height)

答案 2 :(得分:0)

我经常使用的一种非常简单的方法是将您的contentView高度约束与IBOutlet对象连接,并更新其值。

@IBOutlet weak var contentViewHeight : NSLayoutConstraint!

添加标签后:

contentViewHeight.constant += labelHeight

别忘了使用滚动视图将contentView约束(顶部,底部,前导,尾随)设置为0