使用Swift中的UITextView属性自定义UIView的加载时间较慢 [英] Slow load time for custom UIView with UITextView property in Swift
问题描述
我最初问过
但是,如果我注释掉该行
// var textView = UITextView()
然后我自定义视图加载非常快,只有7个。
这里发生了什么?是否可以在自定义视图中使用 UITextView
属性并避免此缓慢加载时间?
尝试了一些类似的东西,其中标签和文本字段被添加为uiview子类的子视图。我的方式如下:
@interface CustomTextField:UIView
@property(weak, nonatomic)IBOutlet UITextField * valueField;
@end
所以,我们有一个xib文件,我们实际上添加标签和文本字段。
在xib文件中,文件所有者是CustomTextField,并且outlet与那里的头文件链接。
构造函数方法如下所示:
- (id)initWithValue:(NSString *)value
{
self = [super initWithFrame:CGRectZero ]。
if(self){
NSArray * nibs = [[NSBundle mainBundle] loadNibNamed:NSStringFromClass([self class])owner:self options:nil];
UIView * view = nibs [0];
self.valueField.frame = view.bounds;
[self setFrame:view.bounds];
[self addSubview:view];
[self.valueField setText:value];
}
返回自我;
}
对我来说没问题。
I originally asked this question. I had assumed that the reason for the slow load time of my custom view was because of layering multiple views on top of each other or perhaps because of some recursion problem. However, after cutting out more and more code to see what would make a difference, it came down to whether I had a UITextView
present or not. Because the apparent source of my problem is so different than what I was expecting in my first question, I decided to start a new question rather than adding a lengthy update to the old one.
I set up my test project with two view controllers. A button on the first view controller calls a show segue to the second view controller. The second view controller has my custom view on it. (Using the second view controller let me get a sense of how long it took to load the custom view.)
Custom view code:
import UIKit
@IBDesignable class UIMongolTextView: UIView {
var textView = UITextView() // key line
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
override init(frame: CGRect){
super.init(frame: frame)
}
}
As you can see, the only difference from a UIView
is that I added a UITextView
property. And it is this custom view that loads very slowly. Running the Allocations tool in Instruments I get the following results (a count of 997):
However, if I comment out the line
//var textView = UITextView()
then I the custom view loads very quickly and only has a count of 7.
What is going on here? Is it possible to use a UITextView
property in a custom view and avoid this slow load time?
Tried something similar where a label and a text field were added as a subviews to a uiview subclass. The way I did was the following:
@interface CustomTextField : UIView
@property (weak, nonatomic) IBOutlet UITextField *valueField;
@end
So, we had a xib file on which we actually add the label and the text field. On the xib file, the file owner is "CustomTextField" and outlets are linked with the header file from there.
The constructor method looks like this:
- (id)initWithValue:(NSString *)value
{
self = [super initWithFrame:CGRectZero];
if (self) {
NSArray *nibs = [[NSBundle mainBundle] loadNibNamed:NSStringFromClass([self class]) owner:self options:nil];
UIView *view = nibs[0];
self.valueField.frame = view.bounds;
[self setFrame:view.bounds];
[self addSubview:view];
[self.valueField setText:value];
}
return self;
}
Works fine for me.
这篇关于使用Swift中的UITextView属性自定义UIView的加载时间较慢的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!