Android Toast in iPhone?

后端 未结 12 875
轮回少年
轮回少年 2020-12-05 20:38

When I write Android apps, I love the Toast feature. Is there a way to get this kind of set and forget popup message in iPhone development using MonoTouch (C# .NET)?

12条回答
  •  旧巷少年郎
    2020-12-05 21:09

    Just You can use the following code with uilabel and uianimation to get toast like in android. It does two works one is toast task and it increases the height of the label according to the text length with wordwrap IOS 7 later link here

    CGRect initialFrame = CGRectMake(20, self.view.frame.size.height/2,300, 40);
    
    
    NSString *message=@"Toast in Iphone as in Android";
    UILabel *flashLabel=[[UILabel alloc] initWithFrame:initialFrame];
    flashLabel.font=[UIFont fontWithName:@"Optima-Italic" size:12.0];
    flashLabel.backgroundColor=[UIColor whiteColor];
    flashLabel.layer.cornerRadius=3.0f;
    flashLabel.numberOfLines=0;
    flashLabel.textAlignment=NSTextAlignmentCenter;
    
    CGSize maxSize = CGSizeMake(flashLabel.frame.size.width, MAXFLOAT);
    
    CGRect labelRect = [message boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:flashLabel.font} context:nil];
    
    //adjust the label the the new height.
    CGRect newFrame = flashLabel.frame;
    newFrame.size.height = labelRect.size.height;
    flashLabel.frame = newFrame;
    flashLabel.text=message;
    [self.view addSubview:flashLabel];
    
    flashLabel.alpha=1.0;
    self.view.userInteractionEnabled=FALSE;
    
    [UIView animateWithDuration:13.0 animations:^
    {
        flashLabel.alpha=0.0f;
    }
    completion:^(BOOL finished)
    {
        self.view.userInteractionEnabled=TRUE;
    
        [flashLabel removeFromSuperview];
    }];
    

提交回复
热议问题