【问题标题】:objective-c ios auto layout NSLayoutConstraintobjective-c ios 自动布局 NSLayoutConstraint
【发布时间】:2022-11-22 01:15:28
【问题描述】:
UIView* headerView = [[UIView alloc] initWithFrame:CGRectMake(8, 8, frameWidth, frameHeight)];
    headerView.backgroundColor = UIColorFromRGB(0x5F70B9);
    
    UIImage* leftImage = [UIImage imageNamed: @"search_list"];
    UIImageView* leftImageView = [[UIImageView alloc] initWithImage: leftImage];
    [leftImageView setFrame: CGRectMake(10, 15, 70, 50)]; // CGRectMake(10, 15, 70, 50)

    UITextView* textView = [[UITextView alloc] initWithFrame:CGRectMake(80, 20, 160, 40)];
    textView.backgroundColor = UIColorFromRGB(0x5F70B9);
    textView.textColor = UIColorFromRGB(0xFFFFFF);
    [textView setFont:[UIFont preferredFontForTextStyle:UIFontTextStyleTitle3]];
    textView.text = STLocalizedString(@"message_overall_non_reply");
    textView.editable = NO;
    
    UIImage* rightImage = [UIImage imageNamed: @"arrow_mask"];
    UIImageView* rightImageView = [[UIImageView alloc] initWithImage: rightImage];
    [rightImageView setFrame:CGRectMake(380, 30, 15, 20)];
    
    [headerView addSubview: leftImageView];
    [headerView addSubview: textView];
    [headerView addSubview: rightImageView];

期望图像: enter image description here

现在我正在对原点 x、y 和所有内容进行硬编码。我想使用自动布局,以便左图像和右图像充当前导和尾随图标。

有什么建议吗?

试过类似下面的东西,但没有工作: `

[leftImageView setTranslatesAutoresizingMaskIntoConstraints:NO];

NSLayoutConstraint *leftImageViewConstraint = [NSLayoutConstraint constraintWithItem: leftImage attribute: NSLayoutAttributeLeading relatedBy: NSLayoutRelationEqual toItem: headerView attribute: NSLayoutAttributeLeading multiplier: 1 constant: 0];

[leftImageView addConstraints: @[leftImageViewConstraint]];

`

【问题讨论】:

    标签: objective-c nslayoutconstraint


    【解决方案1】:

    您需要激活您添加的每个约束。当您有一堆约束要设置时,使用 NSLayoutConstraint activateConstraints 会容易得多。并且使用各种“锚点”属性来设置约束比使用创建 NSLayoutConstraint 的长格式更简单。

    这是您的代码更新了很多约束:

    UIView* headerView = [[UIView alloc] initWithFrame:CGRectMake(8, 8, frameWidth, frameHeight)];
    headerView.backgroundColor = UIColorFromRGB(0x5F70B9);
    
    UIImage* leftImage = [UIImage imageNamed: @"search_list"];
    UIImageView* leftImageView = [[UIImageView alloc] initWithImage: leftImage];
    
    UITextView* textView = [[UITextView alloc] initWithFrame:CGRectZero];
    textView.backgroundColor = UIColorFromRGB(0x5F70B9);
    textView.textColor = UIColorFromRGB(0xFFFFFF);
    textView.font = [UIFont preferredFontForTextStyle:UIFontTextStyleTitle3];
    textView.text = STLocalizedString(@"message_overall_non_reply");
    textView.editable = NO;
    
    UIImage* rightImage = [UIImage imageNamed: @"arrow_mask"];
    UIImageView* rightImageView = [[UIImageView alloc] initWithImage: rightImage];
    
    leftImageView.translatesAutoresizingMaskIntoConstraints = NO;
    textView.translatesAutoresizingMaskIntoConstraints = NO;
    rightImageView.translatesAutoresizingMaskIntoConstraints = NO;
    
    [headerView addSubview: leftImageView];
    [headerView addSubview: textView];
    [headerView addSubview: rightImageView];
    
    [NSLayoutConstraint activateConstraints:@[
        [leftImageView.leadingAnchor constraintEqualToAnchor:headerView.leadingAnchor constant:10],
        //[leftImageView.topAnchor constraintEqualToAnchor:headerView.topAnchor constant:15],
        [leftImageView.centerYAnchor constraintEqualToAnchor:headerView.centerYAnchor],
        [leftImageView.widthAnchor constraintEqualToConstant:70],
        [leftImageView.heightAnchor constraintEqualToConstant:50],
        [rightImageView.trailingAnchor constraintEqualToAnchor:headerView.trailingAnchor constant:-10],
        //[rightImageView.topAnchor constraintEqualToAnchor:headerView.topAnchor constant:30],
        [rightImageView.centerYAnchor constraintEqualToAnchor:headerView.centerYAnchor],
        [rightImageView.widthAnchor constraintEqualToConstant:15],
        [rightImageView.heightAnchor constraintEqualToConstant:20],
        [textView.leadingAnchor constraintEqualToAnchor:leftImageView.trailingAnchor constant:10],
        [textView.trailingAnchor constraintEqualToAnchor:rightImageView.leadingAnchor constant:-10],
        [textView.topAnchor constraintEqualToAnchor:headerView.topAnchor constant:20],
        [textView.bottomAnchor constraintEqualToAnchor:headerView.bottomAnchor constant:-20],
    ]];
    

    我在这里做了一些猜测。显然,您可以更改这些以满足您的需要。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-12-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多