在IOS自动布局问题进行的UITextField [英] Autolayout issue for UITextField in ios

查看:795
本文介绍了在IOS自动布局问题进行的UITextField的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的故事板创建的UITextField 。并增加其约束也。我想搜索的的UITextField左侧的图标。在code添加搜索图标如下:

  self.searchTextField.leftView = searchIconImage;
    self.searchTextField.leftViewMode = UITextFieldViewModeAlways;
    [self.searchTextField addTarget:自我
                  行动:@选择(textFieldDidChange :)
        forControlEvents:UIControlEventEditingChanged];

我的应用程序正在iOS8上及其对iOS7崩溃的罚款。误差如下:


  

在断言失败 - [的UITextField layoutSublayersOfLayer:],/SourceCache/UIKit/UIKit-2935.138/UIView.m:8794
  2014年11月5日12:54:33.377 WattUp [1722:60B]终止应用程序由于未捕获的异常'NSInternalInconsistencyException,理由是:自动布局执行-layoutSubviews后仍然需要。的UITextField的实现-layoutSubviews的需要调用超。



解决方案

所以,我前几天就遇到了这个同样的错误,以及。原来我是想我的的UITextField 子类中,布局子视图,它们设置属性,移动它们,等等,但从来没有明确地告诉视图本身布局(即调用 [自layoutIfNeeded] )。

8的iOS似乎强制以便布局其子视图所有,然后在其上配置的限制。 iOS的7不会,需要你明确地告诉意见,当你改变,如果你使用自动布局重绘其子视图。

在我的情况,我已经子类的UITextField 并添加一个标签到一边。我通过添加约束的UITextField配置的标签的帧。其中一个公共方法我可以打电话给我的课是

   - (无效)setLabelText:(* NSString的)newText {
    self.sideLabel.text = newText;
}

这使我的应用程序崩溃时,视图控制器出现包含我的子类文本框。通过添加 layoutIfNeeded 一切都在iOS7和iOS8上正常工作。

   - (无效)setLabelText:(* NSString的)newText {
    self.sideLabel.text = newText;
    [个体经营layoutIfNeeded]
}

这需要调用每一个您更改子视图的一部分时间。这包括当你添加子视图设置,当您更改视图属性,任何真正。这正在改变你的观点的函数返回前,调用 layoutIfNeeded 你的看法。这似乎适用于包括的UITextField,UITableView的和UICollectionView几个标准的UI控件,但我敢肯定还有其他的。我希望这是不够清楚,帮您解决问题。

你得到的错误是不是超级有用的,甚至没有在我的情况适用。虽然我收到完全相同的错误,没有实现我的看法 layoutSubviews ,因而都使用 [超级layoutSubviews] 方法。

I created UITextField in storyboard. And added its constraints also. I want search icon on left side of UITextField. The code for adding search icon is as follows:

self.searchTextField.leftView = searchIconImage;
    self.searchTextField.leftViewMode = UITextFieldViewModeAlways;
    [self.searchTextField addTarget:self
                  action:@selector(textFieldDidChange:)
        forControlEvents:UIControlEventEditingChanged];

My application is working fine on iOS8 and its crashing on iOS7. The error is as follows:

Assertion failure in -[UITextField layoutSublayersOfLayer:], /SourceCache/UIKit/UIKit-2935.138/UIView.m:8794 2014-11-05 12:54:33.377 WattUp[1722:60b] Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Auto Layout still required after executing -layoutSubviews. UITextField's implementation of -layoutSubviews needs to call super.'

解决方案

So I ran into this same error a few days ago as well. It turns out I was trying to layout subviews inside my UITextfield subclass, setting properties on them, moving them, etc, but was never explicitly telling the view to lay itself out (i.e. calling [self layoutIfNeeded]).

iOS 8 seems to force to a view to layout all its subviews, and then configures constraints on it. iOS 7 won't, and needs you to explicitly tell views to redraw their subviews when you change if you're using autolayout.

In my case, I had subclassed UITextField and added a label to the side. I configured the frame of the label by adding constraints to the UITextfield. One of the public methods I could call on my class was

- (void)setLabelText:(NSString *)newText{
    self.sideLabel.text = newText;
}

This caused my application to crash when a view controller appeared containing my subclassed textfield. By add layoutIfNeeded everything works fine in iOS7 and iOS8.

- (void)setLabelText:(NSString *)newText{
    self.sideLabel.text = newText;
    [self layoutIfNeeded];
}

This needs to be called every time you change a part of the view in your subclass. This includes the setup when you add subviews, when you change view properties, anything really. Before the function that's changing your view returns, call layoutIfNeeded on your view. This seems to apply for a few standard UI controls including UITextfield, UITableView and UICollectionView, though I'm sure there are others. I hope this was clear enough and helped solve your problem.

The error you're getting isn't super useful, and didn't even apply in my case. Though I was receiving the exact same error, none of my views implementing layoutSubviews, and thus were all using the [super layoutSubviews] method.

这篇关于在IOS自动布局问题进行的UITextField的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆