V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
iOS 开发实用技术导航
NSHipster 中文版
http://nshipster.cn/
cocos2d 开源 2D 游戏引擎
http://www.cocos2d-iphone.org/
CocoaPods
http://cocoapods.org/
Google Analytics for Mobile 统计解决方案
http://code.google.com/mobile/analytics/
WWDC
https://developer.apple.com/wwdc/
Design Guides and Resources
https://developer.apple.com/design/
Transcripts of WWDC sessions
http://asciiwwdc.com
Cocoa with Love
http://cocoawithlove.com/
Cocoa Dev Central
http://cocoadevcentral.com/
NSHipster
http://nshipster.com/
Style Guides
Google Objective-C Style Guide
NYTimes Objective-C Style Guide
Useful Tools and Services
Charles Web Debugging Proxy
Smore
hustlzp
V2EX  ›  iDev

[iOS 界面布局求助] tableHeaderView 中有一个 UILabel 的文字内容是变动的,如何实现 tableHeaderView 的高度自适应?

  •  
  •   hustlzp ·
    hustlzp · 2014-07-04 21:25:53 +08:00 · 5561 次点击
    这是一个创建于 3591 天前的主题,其中的信息可能已经有所发展或是发生改变。
    界面布局的需求:

    * UITableView的上方需要有3个UILabel
    * 这3个UILabel需要跟随UITableView一起滚动
    * 其中一个UILabel需要是multiline,其文字是动态变化的,需要自适应高度

    我的做法:

    * 新建一个xib文件,在UIView添加三个UILabel,并对其设置Auto Layout约束
    * 对那个需要multiline的UILabel,设置其numberOfLines为0,然后添加约束:height >= 21
    * 在UITableView中加载此xib,并将其作为UITableView的tableHeaderView

    存在的问题:

    这样做确实是实现了UILabel的multiline显示,但是xib文件中的UIView高度是固定的,然后表格头部就会出现留白...

    之前做Web习惯了CSS布局的灵活,现在学iOS感觉界面布局好折腾人...

    请教一下大家,我的做法中哪里出问题了?或者有更好的方法实现这样的布局需求?
    5 条回复    2014-07-05 15:26:07 +08:00
    txx
        1
    txx  
       2014-07-05 09:46:48 +08:00   ❤️ 1
    有Delegate DataSource不用,非要依賴於IB來實現...
    hustlzp
        2
    hustlzp  
    OP
       2014-07-05 11:02:18 +08:00
    @txx 额,菜鸟,正在学习中...有什么不懂的请指教...
    hustlzp
        3
    hustlzp  
    OP
       2014-07-05 11:54:22 +08:00
    @txx 根据您的提示找到了完美的解决方法,再次感谢!
    NUT
        4
    NUT  
       2014-07-05 13:39:04 +08:00
    别用xib ,frame 全部手写。 可以使用KVO 来监控tb的滚动。
    你可以搜索下 视察滚动。
    hustlzp
        5
    hustlzp  
    OP
       2014-07-05 15:26:07 +08:00
    @NUT 多谢指点。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   978 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 18:31 · PVG 02:31 · LAX 11:31 · JFK 14:31
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.