【问题标题】:Objective C How to Add UiLabel to UiScrollWievObjective C 如何将 UiLabel 添加到 UiScrollWiev
【发布时间】:2011-01-13 13:00:31
【问题描述】:

您好,我可以通过编程方式将 UiLabel 添加到 UiScrollWiev,因为我已阅读 http://developer.apple.com/library/ios/#documentation/uikit/reference/UILabel_Class/Reference/UILabel.html 而且我没有找到任何构造函数。 有没有办法做到这一点?

【问题讨论】:

    标签: objective-c xcode uiscrollview uilabel


    【解决方案1】:
    [myScrollView addSubview:myLabel];
    [myLabel release];
    

    添加视图发生在父(容器)视图中,而不是正在添加的视图中。

    此外,一旦它被添加到 superview,superview 就拥有它,您应该释放它。

    编辑:OP 问道,“但是当我创建 10 个它们时会发生什么情况?”

    没有。每个 UIView 子类(UIScrollView 和 UILabel 都是)都有一个 .frame 属性,其中包含一个 CGRect 结构。该字段定义对象的位置和大小。

    所以:

    UILabel *one = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 200, 30)];
    UILabel *two = [[UILabel alloc] initWithFrame:CGRectMake(0, 35, 200, 30)];
    UILabel *three = [[UILabel alloc] initWithFrame:CGRectMake(0, 70, 200, 30)];
    
    [myScrollView addSubview:one];
    [myScrollView addSubview:two];
    [myScrollView addSubview:three];
    [one release];
    [two release];
    [three release];
    

    这将放置三个 UILabel,每个 200px 宽 x 30px 高在滚动视图上,它们都与该视图的左侧齐平,一个在顶部右侧,一个向下 35 px,一个向下 70 px .

    如果 UIScrollView 的内容大于 UIScrollView 的 .frame,则设置一个 .contentSize 属性(同样是一个 CGRect)来定义您想要滚动的内容区域。

    【讨论】:

    • 非常感谢您的快速回答。但是当我创建其中的 10 个时会发生什么,它们是放在一起的吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-16
    • 2014-11-27
    相关资源
    最近更新 更多