![](/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.