【问题标题】:Stick a UILable to UIView or UIImageView将 UILabel 粘贴到 UIView 或 UIImageView
【发布时间】:2011-12-07 15:40:29
【问题描述】:

我的应用中有一个包含 UIImageView 的子视图。用户可以触摸这个子视图并移动它。我有一些 UILabel 我想“粘贴”或与子视图分组,以便它们随之移动。我想知道该怎么做? 这是我将标签添加到子视图的地方:

- (void)viewDidLoad
{
 [super viewDidLoad];

//Decrease the site of root CALayer of the view and give it round corners
self.view.layer.backgroundColor = [UIColor orangeColor].CGColor;
self.view.layer.cornerRadius = 10.0;
self.view.layer.masksToBounds = YES;
self.view.layer.frame = CGRectInset(self.view.layer.frame, 40, 40);

//check if the background is from the photo library and create a sublayer for the semi transparent white badge and a textsublayer for the countdown labels. Also hide the regular view labels


NSString *path = [_occasion imagePath];

if (path != nil && [path hasPrefix:@"ass"])
{

    //Make a subview with a UIImageView inside for the badge
    CGRect  badgeRect = CGRectMake(32, 13, 236, 222);
    UIView *badge = [[UIView alloc] initWithFrame:badgeRect];
    imageView = [[UIImageView alloc]initWithFrame:[badge frame]];

    [imageView setImage:[UIImage imageNamed:@"badge.png"]];

    [badge addSubview:imageView];

    [self.view addSubview:badge];

    /*[self.view bringSubviewToFront:countDownLabel];
    [self.view bringSubviewToFront:daysLabel];
    [self.view bringSubviewToFront:hoursLabel];
    [self.view bringSubviewToFront:minutesLabel];
    [self.view bringSubviewToFront:secondsLabel];
    [self.view bringSubviewToFront:daysName];
    [self.view bringSubviewToFront:hoursName];
    [self.view bringSubviewToFront:minutesName];
    [self.view bringSubviewToFront:secondsName];*/

    [badge addSubview:countDownLabel];
    [badge addSubview:daysLabel];
    [badge addSubview:hoursLabel];
    [badge addSubview:minutesLabel];
    [badge addSubview:secondsLabel];
    [badge addSubview:daysName];
    [badge addSubview:hoursName];
    [badge addSubview:minutesName];
    [badge addSubview:secondsName];
//more code for recognizing the touches on the subview

【问题讨论】:

    标签: ios uiview uiimageview touch subview


    【解决方案1】:

    只需将它们放在 UIImageView 上,它们就会随之移动。

    【讨论】:

    • 我确实尝试将它们添加到子视图中,但它们没有移动。
    • 不要误会我的意思。您不必将标签放在与 UIImageView 相同的子视图上,而是放在 UIImageView 上。如果它们不应该出现在图像上,请使用带有 UIImageView 和标签的透明容器视图并移动那个。
    • 哦,我确实弄错了。我会尝试将它们添加到 UIImageView 中,看看会发生什么。
    • 是的。那行得通。将 UILabel 添加到 UIImageView 中。请编辑您对 UIImageView 而不是 UIView 的回答,我会接受。非常感谢。这是新代码:[imageView addSubview:countDownLabel]; [imageView addSubview:daysLabel]; [imageView addSubview:hoursLabel]; [imageView addSubview:minutesLabel]; [imageView addSubview:secondsLabel]; [imageView addSubview:daysName]; [imageView addSubview:hoursName]; [imageView addSubview:minutesName]; [imageView addSubview:secondsName];
    【解决方案2】:

    您可以获取子视图的位置并据此更改标签的位置。

    【讨论】:

    • 没有更简单的方法吗?因为我有大约 9 个标签,我想以这种方式保持对齐会有点困难。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多