【发布时间】:2018-06-20 01:40:31
【问题描述】:
我构建了一个自定义 UIView xib,我在 UIViewController 中使用它。整个视图在UINavigationBar 下向上移动。我使用Simulated Metrics 将视图定位在它应该在的位置并使用了
编辑器 > 解决自动布局问题 >(所有视图)重置为建议
但是当我导航到该视图时,该视图被放错了位置:
显然,我错误地实例化了我的子视图。但是哪里?我还必须支持 iOS 9.1 及更高版本。
SearchInputView.h
@interface SearchInputView : UIView
@property (strong, nonatomic) IBOutlet UIView *contentView;
@property (weak, nonatomic) IBOutlet UITextField *textSearch;
@property (weak, nonatomic) IBOutlet UILabel *lblMessage;
@property (weak, nonatomic) IBOutlet UIActivityIndicatorView *indicator;
@property (weak, nonatomic) IBOutlet UIButton *btnSearch;
@end
SearchInputView.m
@implementation SearchInputView
- (instancetype) initWithCoder:(NSCoder *)aDecoder{
self = [super initWithCoder:aDecoder];
if(self){
[self customInit];
}
return self;
}
- (instancetype) initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if(self){
[self customInit];
}
return self;
}
- (void) customInit {
[[NSBundle mainBundle] loadNibNamed:@"SearchInputView" owner:self options:nil];
[self addSubview:self.contentView];
self.contentView.frame = self.bounds;
}
@end
使用模拟指标
还有我的 SearchInputViewController.m
@interface SearchInputViewController () <UITextFieldDelegate> {
SearchInputView * siv;
}
@end
@implementation SearchInputViewController
- (void)viewDidLoad {
[super viewDidLoad];
siv = [[SearchInputView alloc] initWithFrame:self.view.frame];
[self.view addSubview:siv];
siv.textSearch.delegate = self;
[siv.lblMessage setHidden:true];
[siv.indicator stopAnimating];
[siv.btnSearch addTarget:self action:@selector(doSearch) forControlEvents:UIControlEventTouchUpInside];
}
【问题讨论】:
标签: ios objective-c iphone uiview uiviewcontroller