【问题标题】:Controls in UIActionSheet are positioning wrongUIActionSheet 中的控件定位错误
【发布时间】:2010-03-20 17:57:08
【问题描述】:

当用户点击按钮时,我需要显示一个带有 UISwitch 和完成按钮的弹出窗口,并在点击弹出窗口中的完成按钮时保存开关的状态。

所以我使用 UIActionSheet 来执行此操作 -

sheet = [[UIActionSheet alloc] initWithTitle:@"Switch Setting" 
                                              delegate:self
                                     cancelButtonTitle:nil
                                destructiveButtonTitle:nil
                                     otherButtonTitles:@"Done", nil];

theSwitch = [[UISwitch alloc] init];
[sheet addSubview:theSwitch];
[sheet showInView:self.view];


- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == actionSheet.firstOtherButtonIndex)
    {
      theSwitch .on? NSLog(@"Switch is on") : NSLog(@"Switch if off");
    }
}

这一切都很好。我有定位问题。不幸的是,我目前没有可以在此处发布的屏幕截图。但是开关出现在与操作表标题相同的行的最顶部,然后是完成按钮。

请有人帮助我如何将开关放置在标题下方并增加操作表的大小以使其看起来整洁。我需要这个尽快。谢谢。

【问题讨论】:

    标签: objective-c ipad orientation subview uiactionsheet


    【解决方案1】:

    您可以设置开关的框架。

    将开关置于警报视图的中间

    theSwitch = [[UISwitch alloc] init];
    [sheet addSubview:theSwitch];
    [sheet showInView:self.view];
    
    // get the dimension of the sheet to position the switch
    CGSize parentSize  = sheet.frame.size;
    CGRect rect = theSwitch.frame;
    rect.origin.x = parentSize.width/2.0 - rect.size.width;
    rect.origin.y = parentSize.height - rect.size.height - <some space from the bottom>;
    theSwitch.frame = rect;
    

    重要的是,在为工作表调用 showInView 之后定位开关。否则,工作表的大小为零。如果你替换,你可以在底部添加相同的空间。

    希望对你有帮助

    【讨论】:

      【解决方案2】:

      我认为您可以选择通过修改 UISwitch 的框架来实现,就像上面描述的 trumi 一样。如果您想放置带有图像的UIButton,则here 是一个未记录的选项。无证,因此有被拒绝的风险。

      您还可以在same thread 中找到许多其他美化选项

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-26
        • 2015-03-11
        相关资源
        最近更新 更多