简体   繁体   中英

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 .

I have used custom UITableviewCell with xib in that UITableView .

Now I have one UIView with xib and I want to add that UIView's object in custom UITableViewCell with the help of AutoLayout.

I added that UIView successfully in custom UITableViewCell , but that UIView is so much big and it is not fitting in my custom 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.

In My Custom 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.

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/

Hope it helps you...!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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