UIView无法改变尺寸大小怎么办?总是XIB建立的代码。
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了UIView无法改变尺寸大小怎么办?总是XIB建立的代码。相关的知识,希望对你有一定的参考价值。
参考技术A UIView *view = [[[NSBundle mainBundle] loadNibNamed:@"xxxx" owner:nil options:nil] firstObject];view.frame = xxxx;
[self.view addSubView:view];
这种情况, 是把view作为一个单独的文件管理的,
它的结构是(假如叫TestView)
TestView.h
TestView.m
TestView.xib
你还有一种可能, 就是你本身有一个UIViewController, 对应一个xib文件, 在这个viewController内部有一个UIView
而这个UIView你使用了AutoLayout, 所以不能简单的通过改frame来实现, 你应该改的是这个UIView的layout, 也就是对应的NSLayoutConstraint
你看一下你用的是哪种方式来做的本回答被提问者和网友采纳
calayer 没有根据 tableview 单元格中的 UIView 大小而改变
【中文标题】calayer 没有根据 tableview 单元格中的 UIView 大小而改变【英文标题】:calayer did not change according to UIView size in tableview cell 【发布时间】:2017-05-17 05:22:33 【问题描述】:自定义 UITableViewCell 中有一个 view1(UIView),我希望在 AutoLayout 之后绘制新尺寸的 view1(UIView) 来绘制边框
UITableViewCell 的内容是动态的,因此我使用了 UITableViewAutomaticDimension 来自动调整大小
我也使用以下方法,但我得到了原始尺寸。展开后我无法获得 view1(UIView) 的新尺寸
[self.tblView1 setNeedsLayout];
[self.tblView1 layoutIfNeeded];
[cell.view1 setNeedsLayout];
[cell.view1 layoutIfNeeded];
CALayer *rightBorder = [CALayer layer];
rightBorder.backgroundColor = [[UIColor redColor] CGColor];
rightBorder.frame = CGRectMake(cell.view1.frame.size.width-1, 0, 1, cell.view1.frame.size.height);
[cell.view1.layer addSublayer:rightBorder];
rightBorder 是绘制的,但根据 view1 的(UIView)原始尺寸,即 Width = 394 Height = 322 不根据 view1 的(UIView)新尺寸后的布局尺寸,即 Width = 394 Height = 422
【问题讨论】:
你设置了tableView的estimatedRowHeight
属性了吗?
@Rishab 当然是的,兄弟 self.tbl.estimatedRowHeight = 323; self.tbl.rowHeight = UITableViewAutomaticDimension;没有上面的代码,我无法得到动态单元格大小的结果
你是在initWithFrame:
里面写代码还是自定义方法?
@Rishab 你在下面的代码中使用 abt 是的,我写了 rightBorder.frame = CGRectMake(cell.view1.frame.size.width-1, 0, 1, cell.view1.frame.size.height );如果不是,那么我应该写什么 initWithFrame
聊天? chat.***.com/rooms/144410/…
【参考方案1】:
试试这个。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
if(tableView.tag == self.tblEditorialCalendar.tag)
UIView *leftBorder = [UIView new];
leftBorder.backgroundColor = [UIColor redColor];
leftBorder.translatesAutoresizingMaskIntoConstraints = NO;
UIView *topBorder = [UIView new];
topBorder.backgroundColor = [UIColor greenColor];
topBorder.translatesAutoresizingMaskIntoConstraints = NO;
UIView *rightBorder = [UIView new];
rightBorder.backgroundColor = [UIColor blueColor];
rightBorder.translatesAutoresizingMaskIntoConstraints = NO;
UIView *bottomBorder = [UIView new];
bottomBorder.backgroundColor = [UIColor yellowColor];
bottomBorder.translatesAutoresizingMaskIntoConstraints = NO;
[cell addSubview:leftBorder];
[cell addSubview:topBorder];
[cell addSubview:rightBorder];
[cell addSubview:bottomBorder];
[cell addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[leftBorder(3)]"
options:0
metrics:nil
views:NSDictionaryOfVariableBindings(leftBorder)]];
[cell addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[leftBorder]|"
options:0
metrics:nil
views:NSDictionaryOfVariableBindings(leftBorder)]];
[cell addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-3-[topBorder]|"
options:0
metrics:nil
views:NSDictionaryOfVariableBindings(topBorder)]];
[cell addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[topBorder(1)]"
options:0
metrics:nil
views:NSDictionaryOfVariableBindings(topBorder)]];
[cell addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:[rightBorder(1)]|"
options:0
metrics:nil
views:NSDictionaryOfVariableBindings(rightBorder)]];
[cell addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[rightBorder]|"
options:0
metrics:nil
views:NSDictionaryOfVariableBindings(rightBorder)]];
[cell addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-3-[bottomBorder]|"
options:0
metrics:nil
views:NSDictionaryOfVariableBindings(bottomBorder)]];
[cell addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[bottomBorder(1)]|"
options:0
metrics:nil
views:NSDictionaryOfVariableBindings(bottomBorder)]];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
return cell;
另一种方法是使用布局锚(iOS 9 及更高版本支持)而不是 VFL
[leftBorder.widthAnchor constraintEqualToConstant:3].active = YES;
[cell.leadingAnchor constraintEqualToAnchor:leftBorder.leadingAnchor constant:0].active = YES;
[cell.topAnchor constraintEqualToAnchor:leftBorder.topAnchor constant:0].active = YES;
[cell.bottomAnchor constraintEqualToAnchor:leftBorder.bottomAnchor constant:0].active = YES;
[topBorder.heightAnchor constraintEqualToConstant:1].active = YES;
[cell.leadingAnchor constraintEqualToAnchor:topBorder.leadingAnchor constant:3].active = YES;
[cell.topAnchor constraintEqualToAnchor:topBorder.topAnchor constant:0].active = YES;
[cell.trailingAnchor constraintEqualToAnchor:topBorder.trailingAnchor constant:0].active = YES;
[rightBorder.widthAnchor constraintEqualToConstant:1].active = YES;
[cell.trailingAnchor constraintEqualToAnchor:rightBorder.trailingAnchor constant:0].active = YES;
[cell.topAnchor constraintEqualToAnchor:rightBorder.topAnchor constant:0].active = YES;
[cell.bottomAnchor constraintEqualToAnchor:rightBorder.bottomAnchor constant:0].active = YES;
[bottomBorder.heightAnchor constraintEqualToConstant:1].active = YES;
[cell.leadingAnchor constraintEqualToAnchor:bottomBorder.leadingAnchor constant:3].active = YES;
[cell.trailingAnchor constraintEqualToAnchor:bottomBorder.trailingAnchor constant:0].active = YES;
[cell.bottomAnchor constraintEqualToAnchor:bottomBorder.bottomAnchor constant:0].active = YES;
或者您可以只在您的 nib 文件中获取四个视图(作为四个边框)并将它们放置在适当的约束条件下。
【讨论】:
以上是关于UIView无法改变尺寸大小怎么办?总是XIB建立的代码。的主要内容,如果未能解决你的问题,请参考以下文章
Xcode4.2怎么设置 view 视图的大小,使它按照ipad的实际尺寸显示?
对于直接从 Xib 加载的 UIView,何时应用 UIView 大小类?