gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 01:58:13 24 4
gpt4 key购买 nike

我有一个 View Controller ,其中有一个 UITableView

我在那个 UITableView 中使用了带有 xib 的自定义 UITableviewCell

现在我有一个带有 xib 的 UIView,我想在 AutoLayout 的帮助下在自定义 UITableViewCell 中添加那个 UIView 的 对象。

我在自定义 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,但在设备中它比我想要的尺寸大得多。

谢谢。

最佳答案

您可以使用以下代码添加宽度和高度限制

    -(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/

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

关于ios - 如何借助 AutoLayout 在自定义 UITableviewCell 中添加自定义 UIView(xib)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30611855/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com