简体   繁体   English

如何借助AutoLayout在自定义UITableviewCell中添加自定义UIView(xib)?

[英]How to add custom UIView(xib) in custom UITableviewCell with the help of AutoLayout?

I have one View Controller in which I have one UITableView . 我有一个View Controller,其中有一个UITableView

I have used custom UITableviewCell with xib in that UITableView . 我在UITableView中将自定义UITableviewCell与xib一起使用。

Now I have one UIView with xib and I want to add that UIView's object in custom UITableViewCell with the help of AutoLayout. 现在,我有一个带有xib的UIView ,我想借助AutoLayout在自定义UITableViewCell添加该UIView's对象。

I added that UIView successfully in custom UITableViewCell , but that UIView is so much big and it is not fitting in my custom UITableViewCell . 我已在自定义UITableViewCell成功添加了UIView ,但该UIView太大它不适合我的自定义 UITableViewCell

I know that UIView can be fit properly in custom UITableViewCell with the help of AutoLayout, but I don't know How, because I am new to AutoLayout. 我知道在AutoLayout的帮助下UIView可以正确地适合自定义UITableViewCell ,但是我不知道如何使用,因为我是AutoLayout的新手。

In My Custom UITableViewCell 在我的自定义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];
}

Here my vwJob.frame.size.height = 90 , but in device it is much bigger that the size i want. 在这里,我的vwJob.frame.size.height = 90 ,但是在设备中,它比我想要的大小大得多。

Thanks. 谢谢。

You can add width and height constraint using below code 您可以使用以下代码添加宽度和高度约束

    -(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];

    }

Source: 资源:

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/ https://codehappily.wordpress.com/2013/09/21/constant-height-width-constraint-autolayout/ https://codehappily.wordpress.com/2013/10/09/ios-how-to-programmatically-附加自动布局约束换一个视角,即意志-装修公司-上海华/

Hope it helps you...! 希望它可以帮助您...!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM