【问题标题】:Using Simulated Metrics and AutoLayout fails to Correctly Account for Subviews使用模拟指标和 AutoLayout 无法正确考虑子视图
【发布时间】: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


    【解决方案1】:

    你可以试试自动布局更好

    将此添加到您的课程中

    + (id) newFakeHUD
    {
        UINib *nib = [UINib nibWithNibName:@"SearchInputView" bundle:nil];
        NSArray *nibArray = [nib instantiateWithOwner:self options:nil];
        SearchInputView*me = [nibArray objectAtIndex: 0];
        return me;
    }
    

    在 viewDidLoad 中

     re = [SearchInputView newFakeHUD]
    
     [self.view addSubview:re]
    
      re.frame = self.view.frame
    

     -(void)viewDidLayoutsubviews()
    { 
      if(once)
      {
          once = NO;     
    
          self.siv.translatesAutoresizingMaskIntoConstraints = NO;   
    
          NSLayoutConstraint*con1=[NSLayoutConstraint constraintWithItem:self.siv attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeCenterX multiplier:1.0 constant:0.0];        
    
          NSLayoutConstraint*con2=[NSLayoutConstraint constraintWithItem:self.siv attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeCenterY multiplier:1.0 constant:0.0];
    
          NSLayoutConstraint*con3=[NSLayoutConstraint constraintWithItem:self.siv attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeHeight multiplier:1.0 constant:0.0];        
    
          NSLayoutConstraint*con4=[NSLayoutConstraint constraintWithItem:self.siv attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeWidth multiplier:1.0 constant:0.0];
    
          [self.view addConstraints:@[con1,con2,con3,con4]];
    
          [self.view.layoutIfNeeded]
    
        }
    
    }
    

    【讨论】:

    • 你应该先在 viewDidload 处将 siv 添加到 self.view
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-05
    相关资源
    最近更新 更多