由于我期望性能更好,因此每个textFIEld只使用一个inputVIEw实例.这样创建必须只发生一次,这使得有时需要直接访问inputVIEw更容易. inputVIEw设置在 – (BOol)textFIEldShouldBeginEditing:(UITextFIEld *)textFIEld中,设置其所需的高度并将显示.
这非常有效,但不适用于iOS8.有一些包含inputVIEw的系统视图在更改时不会更新其框架以匹配inputVIEw的边界(第一次工作).
我知道可以通过每个textFIEld使用我的inputVIEw的一个实例来修复.但我问是否有一种推荐/更好的方法来调整框架或将其更改报告给包含视图.也许这是一个iOS8错误,可以修复直到发布?
以下是重现问题的示例代码:
CustominputVIEw
@implementation CustominputVIEw+ (CustominputVIEw*)sharedinputVIEw{ static CustominputVIEw *sharedInstance; static dispatch_once_t oncetoken; dispatch_once(&oncetoken,^{ sharedInstance = [[CustominputVIEw alloc] init]; }); return sharedInstance;}- (ID)init{ self = [super init]; if (self) { self.backgroundcolor = [UIcolor greencolor]; } return self;}- (voID)setupForTextFIEld:(UITextFIEld*)textFIEld{ CGfloat height; if(textFIEld.tag == 1){ height = 100; }else height = 50; self.frame = CGRectMake(0,320,height);}@end
TestVIEwController代码
- (voID)vIEwDIDLoad{ [super vIEwDIDLoad]; UITextFIEld *tf = [[UITextFIEld alloc] initWithFrame:CGRectMake(15,50,290,30)]; tf.text = @"bigKeyboard"; tf.inputVIEw = [CustominputVIEw sharedinputVIEw]; tf.layer.borderWIDth = 1; tf.layer.bordercolor = [UIcolor lightGraycolor].CGcolor; tf.delegate = self; tf.tag = 1; [self.vIEw addSubvIEw:tf]; tf = [[UITextFIEld alloc] initWithFrame:CGRectMake(15,100,30)]; tf.text = @"smallKeyboard"; tf.inputVIEw = [CustominputVIEw sharedinputVIEw]; tf.layer.borderWIDth = 1; tf.layer.bordercolor = [UIcolor lightGraycolor].CGcolor; tf.delegate = self; tf.tag = 2; [self.vIEw addSubvIEw:tf]; UIbutton *button = [UIbutton buttonWithType:UIbuttonTypeSystem]; [button setTitle:@"dismissKeyboard" forState:UIControlStatenormal]; [button addTarget:self action:@selector(endEditing) forControlEvents:UIControlEventtouchUpInsIDe]; button.frame = CGRectMake(15,150,30); [self.vIEw addSubvIEw:button];}- (voID)endEditing{ [self.vIEw endEditing:YES];}- (BOol)textFIEldShouldBeginEditing:(UITextFIEld *)textFIEld{ [[CustominputVIEw sharedinputVIEw] setupForTextFIEld:textFIEld]; return YES;}解决方法 我在将自定义键盘从iOS 8调整到iOS 10时遇到了类似的问题.我认为正确的解决方案是让输入视图提供适当的intrinsicContentSize并在您想要更改视图高度时更改(并使其无效!)该值.示例代码:
class CustominputVIEw: UIinputVIEw { var intrinsicHeight: CGfloat = 200 { dIDSet { self.invalIDateIntrinsicContentSize() } } init() { super.init(frame: CGRect(),inputVIEwStyle: .keyboard) self.translatesautoresizingMaskIntoConstraints = false } required init?(coder: NSCoder) { super.init(coder: coder) self.translatesautoresizingMaskIntoConstraints = false } overrIDe var intrinsicContentSize: CGSize { return CGSize(wIDth: UIVIEwNoIntrinsicmetric,height: self.intrinsicHeight) }}class VIEwController: UIVIEwController { @IBOutlet weak var textVIEw: UITextVIEw! overrIDe func vIEwDIDLoad() { super.vIEwDIDLoad() textVIEw.becomeFirstResponder() let inputVIEw = CustominputVIEw() // To make the vIEw's size more clear. inputVIEw.backgroundcolor = UIcolor(red: 0.5,green: 1,blue: 0.5,Alpha: 1) textVIEw.inputVIEw = inputVIEw // To demonstrate a change to the vIEw's intrinsic height. dispatchQueue.main.asyncAfter(deadline: dispatchTime.Now() + .seconds(2)) { inputVIEw.intrinsicHeight = 400 } }}
另见https://stackoverflow.com/a/40359382/153354.
总结以上是内存溢出为你收集整理的iOS 8中具有动态高度的自定义inputView全部内容,希望文章能够帮你解决iOS 8中具有动态高度的自定义inputView所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)