![](/img/trans.png)
[英]SWIFT: How to set the frame size of a collection view programmatically so that it's equal to its parent view?
[英]How to make a programmatically added view size to its parent view?
首先,我们需要从布局更改中了解大小更改,我们可以这样进行:
override var bounds: CGRect {
didSet {
print("Bounds have changed: \(bounds)")
}
}
这应该在您的自定义键盘视图中使用。 接下来,我们要更新自定义键盘布局。 查看您的屏幕快照,这表明我们需要做的就是调整键的高度和宽度,稍后您可以根据大小类的更改添加调整,从而更好地控制布局。 您将需要使用@IBOutlets
或通过遍历子视图并手动查找和更改宽度和高度约束来保留对高度和宽度约束的引用,这两者都是很繁琐的,但是使用@IBOutlets
更容易。
func updateKeys() {
DispatchQueue.main.async {
// Create array of width constraints
let widthConstraints: [NSLayoutConstraint] = widthArray()
widthConstraints.forEach { _ in /* change width constant for current layout */ }
// Create array of height constraints
let heightConstraints: [NSLayoutConstraint] = heightArray()
heightConstraints.forEach { _ in /* change height constant for current layout */ }
}
}
这只是一种方法,我发现对于自动布局,在复杂的布局中总会有一种方法,但是它应该向您展示可以使用的模式。
您是否尝试实施这些通知?
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardFrameWillChange:)
name:UIKeyboardWillShowNotification
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardFrameWillChange:)
name:UIKeyboardWillChangeFrameNotification
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:)
name:UIKeyboardWillHideNotification
object:nil];
然后,在调用这些通知时,将视图大小调整为键盘框架
CGRect keyboardFrame;
[[notification.userInfo valueForKey:UIKeyboardFrameEndUserInfoKey] getValue:&keyboardFrame];
CGRect viewFrame = keyboardFrame;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.