【问题标题】:How to show comment box as popup in iOS?如何在iOS中将评论框显示为弹出窗口?
【发布时间】:2016-01-12 01:42:34
【问题描述】:

我想显示一个包含 UITableView 作为顶视图的视图,该视图将显示用户发布的 cmets 和 UITextField 以及下方的按钮,该按钮将进入评论并将其添加到帖子中,我在 facebook 应用程序中看到,当我按下评论按钮时,会出现一个弹出窗口。所以我怎样才能以最好的方式实现。我知道我可以通过在父视图中添加一个视图来做到这一点动画。但这将导致我添加更多和更多的自定义代码。那么有没有其他方法可以实现这个?

【问题讨论】:

  • 为您的概念使用 textview
  • 我在这里讨论如何显示弹出窗口
  • 您在寻找 iPhone 或 iPad 解决方案吗?对于 iPad,有现有的 API,您可以在其中以模态方式呈现视图控制器。但是 iPhone 没有,你需要使用 3rd 方库或你自己的。
  • 我正在寻找两者

标签: ios objective-c iphone uitableview ipad


【解决方案1】:

PopUp 的库太多了,你可以根据需要添加。 最好的是 -

1.https://github.com/jmascia/KLCPopup

还有一个庞大的开源 iOS 库库

2.https://www.cocoacontrols.com/search?q=popup

试一试,希望对你有帮助。

以编程方式更新

添加这些属性:-

@property (strong, nonatomic) UIView *container;
@property (strong, nonatomic) UIView *pop;

@synthesize 它们。

设置带有阴影效果的弹出窗口:-

-(void)setViewPop{
    _container  = [[UIView alloc] initWithFrame:self.view.frame];
    [self.view addSubview:_container];
    //self.backgroundDimmingView = [self buildBackgroundDimmingView];
    //[self.container addSubview:self.backgroundDimmingView];

    pop = [[UIView alloc] initWithFrame:CGRectMake(0, self.view.frame.size.height , self.view.frame.size.width-80, self.view.frame.size.height/1.5-40 )];
    pop.backgroundColor = [UIColor orangeColor];
    pop.layer.cornerRadius = 10;

    UIBezierPath *shadowPath = [UIBezierPath bezierPathWithRect:pop.bounds];
    pop.layer.masksToBounds = NO;
    pop.layer.shadowRadius = 5;
    pop.layer.shadowColor = [UIColor whiteColor].CGColor;
    pop.layer.shadowOffset = CGSizeMake(0.0f, 5.0f);
    pop.layer.shadowOpacity = 0.5f;
    pop.layer.shadowPath = shadowPath.CGPath;

    [self.container addSubview:pop];
    [self setUpPop];

    pop.center = self.container.center;

}

弹出视图的内容;-

-(void)setUpPop{
    self.pop.frame = CGRectMake(0, 0, self.view.frame.size.width-80, self.view.frame.size.height/1.5-40 );


    UILabel *title = [[UILabel alloc] initWithFrame:CGRectMake(90, 10, 100, 20)];
    title.textColor = [UIColor whiteColor];
    title.textAlignment = NSTextAlignmentCenter;
    title.text = @"Choose Date";

    [self.pop addSubview:title];

    UIButton *cancelbutton = [[UIButton alloc] initWithFrame:CGRectMake(0,self.pop.frame.size.height-40, self.pop.frame.size.width/2, 40)];
    UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:cancelbutton.bounds byRoundingCorners:( UIRectCornerBottomLeft) cornerRadii:CGSizeMake(10.0, 10.0)];

    CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
    maskLayer.frame = self.view.bounds;
    maskLayer.path  = maskPath.CGPath;
    cancelbutton.layer.mask = maskLayer;
    cancelbutton.backgroundColor = [UIColor lightGrayColor];
    [cancelbutton setTitle:@"Cancel" forState:UIControlStateNormal];
    [cancelbutton addTarget:self
                     action:@selector(cancelbuttonTapped:)
           forControlEvents:UIControlEventTouchUpInside];
    [pop addSubview:cancelbutton];

    UIButton *confirmbutton = [[UIButton alloc] initWithFrame:CGRectMake(140, self.pop.frame.size.height-40,self.pop.frame.size.width/2, 30)];
    UIBezierPath *maskPath1 = [UIBezierPath bezierPathWithRoundedRect:confirmbutton.bounds byRoundingCorners:( UIRectCornerBottomRight) cornerRadii:CGSizeMake(10.0, 10.0)];

    CAShapeLayer *maskLayer1 = [[CAShapeLayer alloc] init];
    maskLayer1.frame = self.view.bounds;
    maskLayer1.path  = maskPath1.CGPath;
    confirmbutton.layer.mask = maskLayer1;

    confirmbutton.backgroundColor = [UIColor orangeColor];
    confirmbutton.alpha=0.8f;
    [confirmbutton setTitle:@"set Date" forState:UIControlStateNormal];
    [pop addSubview:confirmbutton];

}

这是显示视图的动画;-

- (void)showPopUp{

    _container.transform = CGAffineTransformMakeScale(0.01, 0.01);
    [UIView animateWithDuration:0.5f delay:0 usingSpringWithDamping:0.7f initialSpringVelocity:3.0f options:UIViewAnimationOptionAllowAnimatedContent  animations:^{
        _container.transform = CGAffineTransformIdentity;
        [self setViewPop];
    } completion:^(BOOL finished){
        // do something once the animation finishes, put it here
    }];

}

在按钮单击或文本字段上调用 ​​-(void)showPopUp 方法。根据您的需要。

【讨论】:

  • 如果我不想使用第三方库怎么办。我想以模态方式呈现视图控制器
  • Apple(existing API) 不支持 iPhone 上的模态视图控制器。
  • ok,你可以使用 uiPopOverController 来显示弹窗,也可以通过程序添加一个带有弹窗动画的视图。
  • UIPopOver 在 iPhone 上不可用
  • 那么你可以通过编程实现一个 UIView
【解决方案2】:

同样有很多第三方库。 你可以看看其中的一些。 http://www.code4app.net/category/popupview

我个人会推荐 CTPopOutMenu。 http://www.code4app.net/ios/his-control-is-like-an-UIAlertView-with-button-icon-and-four-basic-layout/54fd4759e24741fd64073341

我用过它,它对我来说就像一个魅力。 快乐编码.. :)

【讨论】:

    【解决方案3】:

    如果是用于 iPad(如图所示),请尝试创建一个新的 UIViewController 并从您当前的 viewController 模态显示它,但将 UIModalPresentationStyle 属性设置为 UIModalPresentationFormSheet

    【讨论】:

    • 如果设备不是 ipad 将如何在 iPhone 上显示
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多