【问题标题】:How to add custom UIView(xib) in custom UITableviewCell with the help of AutoLayout?如何借助 AutoLayout 在自定义 UITableviewCell 中添加自定义 UIView(xib)?
【发布时间】:2015-06-03 05:53:07
【问题描述】:

我有一个视图控制器,其中有一个UITableView

我在那个UITableView中使用了自定义UITableviewCell和xib。

现在我有一个带有 xib 的 UIView,我想借助 AutoLayout 将 UIView's 对象添加到自定义 UITableViewCell 中。

我在自定义UITableViewCell 中成功添加了UIView,但是UIView 太大了 并且它不适合我的自定义 UITableViewCell .

我知道 UIView 可以在 AutoLayout 的帮助下正确适应自定义 UITableViewCell,但我不知道如何,因为我是 AutoLayout 的新手。

在我的自定义UITableViewCell

CustomAlertCell.m//this is my custom UITableViewCell class

-(void)addCustomView_ForRow:(int)theRow
{
    JobAlertCell_View *vwJob = [[JobAlertCell_View alloc] initFromNibFile:@"JobAlertCell_View"];
   vwJob.frame = CGRectMake(0, 118, vwJob.frame.size.width, vwJob.frame.size.height);
   [self addSubview:vwJob];
}

这是我的vwJob.frame.size.height = 90,但在设备中它比我想要的大得多。

谢谢。

【问题讨论】:

  • 是您的UIView 变量的尺寸吗?
  • 不,我的自定义 UIVIew 的高度和宽度是固定的。

标签: ios objective-c uitableview uiview autolayout


【解决方案1】:

您可以使用以下代码添加宽度和高度约束

    -(void)addCustomView_ForRow:(int)theRow
    {
       JobAlertCell_View *vwJob = [[JobAlertCell_View alloc] initFromNibFile:@"JobAlertCell_View"];
       [self addSubview:vwJob];

        [self addConstraint:[NSLayoutConstraint constraintWithItem:vwJob
                                                       attribute:NSLayoutAttributeHeight
                                                       relatedBy:NSLayoutRelationEqual
                                                          toItem:nil
                                                       attribute:NSLayoutAttributeNotAnAttribute
                                                      multiplier:1.0
                                                        constant:100.0]];
        [self addConstraint:[NSLayoutConstraint constraintWithItem:vwJob
                                                       attribute:NSLayoutAttributeWidth
                                                       relatedBy:NSLayoutRelationEqual
                                                          toItem:nil
                                                       attribute:NSLayoutAttributeNotAnAttribute
                                                      multiplier:1.0
                                                        constant:100.0]];
        [self layoutIfNeeded];

    }

来源:

https://codehappily.wordpress.com/2013/09/21/constant-height-width-constraint-autolayout/ https://codehappily.wordpress.com/2013/10/09/ios-how-to-programmatically-add-auto-layout-constraints-for-a-view-that-will-fit-its-superview/

希望对您有所帮助...!

【讨论】:

  • 我试过你的代码,但仍然是同样的问题。我低于警告。无法同时满足约束。可能至少以下列表中的一个约束是您不想要的。"<0x7fb260c9faf0 h:><0x7fb260d50040 h="-&-" v="-&" jobalertcell_view:0x7fb260ca00b0.width="=" jobalert_cell:0x7fb260c973c0.width><0x7fb260d510d0 h:>
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-05-16
  • 2017-10-01
  • 1970-01-01
  • 2015-04-17
  • 2015-04-01
相关资源
最近更新 更多