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
refresh
V2EX  ›  iDev

在IB中,UILabel怎样自动大小?一直用代码构建UI,不懂IB啊

  •  
  •   refresh · 2013-07-15 18:04:55 +08:00 · 3427 次点击
    这是一个创建于 3947 天前的主题,其中的信息可能已经有所发展或是发生改变。
    尝试使用IB,我想要在一个UIView中放入一个UILabel,然后UILabel根据UIView的大小自动改变大小。
    我设置了UIView的Autoresize subviews,并且设置了UILabel的Autosizing,但不起作用,我想是什么地方没有设置好。

    实际上我要的就是这样的效果:

    lblText.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleLeftMargin;
    10 条回复    1970-01-01 08:00:00 +08:00
    alexrezit
        1
    alexrezit  
       2013-07-15 18:12:19 +08:00
    你到底想保持哪些地方的距离或尺寸不变?
    fsw90628
        2
    fsw90628  
       2013-07-15 18:19:28 +08:00
    只知道用代码可以。

    sizeToFit
    refresh
        3
    refresh  
    OP
       2013-07-15 18:23:30 +08:00
    @alexrezit UILabel(子)根据UIView(父)等比放大,如UILabel={0, 0, 50, 50},UIView ={0,0, 100, 100},如果 UIView={0,0,200,200},则UILabel={0,0,100,100}。
    IB太能驾驭了...
    PrideChung
        4
    PrideChung  
       2013-07-15 18:44:35 +08:00
    IB里面的边距勾上了的话代表是固定的,不勾才是UIViewAutoresizingFlexible{Top,Right,Bottom,Left}Margin, 你如果是需要Label铺满它的父级View的话,把UILabel的固定边距和可变高度,可变宽度全部勾上,其实边距是否可变在这种情况下不重要。



    详细看这个回答。

    http://stackoverflow.com/questions/10468389/uiview-autoresizingmask-interface-builder-to-code-programmatically-create-st
    alexrezit
        5
    alexrezit  
       2013-07-15 19:33:56 +08:00
    @refresh
    等比用 autoresizing mask 似乎是不行的. 如果需要做复杂的缩放建议在 -layoutSubviews 里面写.
    PrideChung
        6
    PrideChung  
       2013-07-15 19:48:03 +08:00
    @refresh 才发现你要等比缩放…… 只能自己重载 -layoutSubviews 算尺寸和位置, 或者用Auto Layout。
    alexrezit
        7
    alexrezit  
       2013-07-15 19:56:09 +08:00
    @PrideChung
    Auto Layout 改起来麻烦死.
    PrideChung
        8
    PrideChung  
       2013-07-15 20:19:01 +08:00
    @alexrezit 对于简单的布局的确是杀鸡用牛刀,复杂的布局才能体现Auto Layout的强大。最近发现xib有个好处是可以分别设置每个xib是否启用Auto Layout,Storyboard只能整个设置。
    alexrezit
        9
    alexrezit  
       2013-07-15 20:38:52 +08:00
    @PrideChung
    强大是强大. 可用代码写起来更快维护也更方便啊...
    PrideChung
        10
    PrideChung  
       2013-07-15 20:44:08 +08:00
    @alexrezit Auto Layout一样可以用代码写啊,还可以用Visual Format来简化布局的代码。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5881 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 01:46 · PVG 09:46 · LAX 18:46 · JFK 21:46
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.