Comments (4)
- If you use TGLinearLayout to the UITableView's tableHeaderView and want to get runtime height, you can coding like below:
let tableHeaderViewLayout = TGLinearLayout(.vert)
tableHeaderViewLayout.tg_padding = UIEdgeInsetsMake(10, 10, 10, 10)
//frame's width must be exact and height can be zero。
tableHeaderViewLayout.frame = CGRect(x: 0, y: 0, width: self.tableView.bounds.width, height: 0)
tableHeaderViewLayout.tg_width.equal(.fill)
tableHeaderViewLayout.tg_height.equal(.wrap) //height is wrap
let label1 = UILabel()
label1.text = NSLocalizedString("add tableHeaderView(please touch me)", comment: "")
label1.tg_centerX.equal(0)
label1.sizeToFit()
tableHeaderViewLayout.addSubview(label1)
let label2 = UILabel()
label2.text = NSLocalizedString(" if you use layout view to realize the dynamic height tableHeaderView, please use frame to set view's width and use wrapContentHeight to set view's height. the layoutIfNeeded method is needed to call before the layout view assignment to the UITableview's tableHeaderView.", comment: "")
label2.tg_leading.equal(5)
label2.tg_trailing.equal(5)
label2.tg_height.equal(.wrap)
label2.tg_top.equal(10)
tableHeaderViewLayout.addSubview(label2)
//Because tableHeaderViewLayout's height is wrap, so you must call layoutIfNeeded before to set to the tableView.tableHeaderView. layoutIfNeeded may calculate the TGLinearLayout's runtime frame.
tableHeaderViewLayout.layoutIfNeeded()
self.tableView.tableHeaderView = tableHeaderViewLayout
- If you use TGLinearLayout to the UITableViewCell and want to get runtime height, you can coding like below:
step 1: set the tableview's estimatedRowHeight and rowHeight
in the viewDidLoad
self.tableView.estimatedRowHeight = 60
self.tableView.rowHeight = UITableViewAutomaticDimension
step2: derive a class from UITableViewCell, and override init like below:
override init(style: UITableViewCellStyle, reuseIdentifier: String?)
{
super.init(style: style, reuseIdentifier: reuseIdentifier)
self.rootLayout = TGLinearLayout(.horz)
self.rootLayout.tg_width.equal(.fill)
self.rootLayout.tg_height.equal(.wrap) //set rooLayout' height is wrap
self.rootLayout.tg_cacheEstimatedRect = true //cache will improve performance.
self.contentView.addSubview(self.rootLayout)
// you can add any subviews to the self.rootLayout
}
step3: override the UITableViewCell's systemLayoutSizeFitting
:
override func systemLayoutSizeFitting(_ targetSize: CGSize, withHorizontalFittingPriority horizontalFittingPriority: UILayoutPriority, verticalFittingPriority: UILayoutPriority) -> CGSize
{
return self.rootLayout.sizeThatFits(targetSize)
}
then the UITableViewCell'height will be auto calculated!!
My English is poor, file: AllTest1ViewController.swift and AllTest1TableViewCell.swift are detailed introduction how to use TGLayout with UITableView.
from tangramkit.
nice shot
from tangramkit.
Here is the code:
Table view:
self.tableView.register(NotificationCell.self, forCellReuseIdentifier: "notification");
self.tableView.separatorStyle = .none;
let bgImageView = UIImageView(image: #imageLiteral(resourceName: "innerbg"));
bgImageView.contentMode = .scaleAspectFill
self.tableView.backgroundView = bgImageView;
self.tableView.estimatedRowHeight = 60
self.tableView.rowHeight = UITableViewAutomaticDimension
self.tableView.numberOfSectionsIn {
return 1;
}
self.tableView.numberOfRows { section in
return self.notifications.count;
}
/*self.tableView.heightForRowAt { indexPath in
return self.configureHeight(for: indexPath);
}*/
self.tableView.cellForRow{ indexPath in
return self.configureCell(for: indexPath);
}
Cell:
https://pastebin.com/pX4bsUZz
Please help me. What I am doing wrong?
from tangramkit.
- First: in your
initView
method, if your root layout usetg_horzMargin(16)
means that the root layout's real width equal to self.contentView.frame.width - 32. It will conflict withtg_width.equal(.fill)
. The same if your root layout usetg_vertMargin(8) means that the root layout's height equal to self.contentView.frame.height - 8. It will conflict with
tg_height.equal(.wrap)`。 Because your cell's height will dynamic height。So you can change to :
func initViews(){
.....
let root = TGLinearLayout(.horz);
// root.tg_width.equal(.fill)
root.tg_height.equal(.wrap)
root.tg_horzMargin(16.0);
// root.tg_vertMargin(8.0);
root.tg_top.equal(8.0); //use tg_top instead to tg_horzMargin set the top margin.
.....
- Second: You must change
systemLayoutSizeFitting
as below:
override func systemLayoutSizeFitting(_ targetSize: CGSize, withHorizontalFittingPriority horizontalFittingPriority: UILayoutPriority, verticalFittingPriority: UILayoutPriority) -> CGSize
{
var targetSize2 = targetSize
//Because you root layout's real width is self.contentView.frame.width - 32, so you must sub 32.
targetSize2.width -= 32;
var size = self.root!.sizeThatFits(targetSize2)
size.height += 16 //The size is root layout's size not cell's size, so height must add 2*8 = 16。(top and bottom margin are 8)
return size;
}
from tangramkit.
Related Issues (20)
- 布局动画问题 HOT 2
- tg_setSubviews失效
- 你好,我是跟着你的框架学的ios开发,有一个问题想请教一下 HOT 4
- 希望能实现tg_min功能,让视图宽度等于多个视图宽度的最小值,谢谢 HOT 1
- iOS 13 TGBorderline 未适配 Dark Model HOT 1
- 测试环境iOS13, FlowLayout 的 对齐问题 HOT 1
- 在Controller中合适才可以拿到准确的frame? HOT 2
- 【BUG】非布局视图layoutSubviews相关 HOT 1
- 【需求】流动布局`TGFloatLayout`添加动态边距 HOT 1
- 【需求】FlexLayout支持 HOT 1
- 【BUG】布局视图响应事件并未传递 HOT 1
- 请问为什么不更新了 HOT 4
- CELL复用问题 HOT 1
- mach-o包体积过大, 远超snapkit HOT 1
- iOS 15 上 UITableViewCell 自适应 cell 发生某一行加载不出来
- 布局高度问题,希望还能得到作者回复 HOT 4
- 布局高度问题,希望作者解答一下 HOT 14
- There are quite a lot of functions
- iphone 12以上,scrollview中的layout隐藏视图,页面卡死 HOT 1
- install TangramKit,Insufficient authority!
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from tangramkit.