UIActionSheet 中的控件定位错误

发布于 2024-08-25 17:17:29 字数 967 浏览 6 评论 0原文

当用户点击按钮时,我需要显示带有 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");
    }
}

所有这些都很好。我定位有问题。不幸的是,我目前没有屏幕截图可以发布在这里。但该开关出现在与操作表标题同一行的最顶部,然后在其下方是“完成”按钮。

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

I need to display a pop with a UISwitch and a Done button when user taps on a button and save the state of the switch when the done button in the pop up is tapped.

So I use UIActionSheet to do this -

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");
    }
}

All this works great. I have problem with positioning. Unfortunately, I don't have a screen shot at the moment to post here. But the switch appears at the very top on the same line with the Action Sheet title and then below that is the Done button.

Please someone help me how to position the switch below the Title and increase the Action Sheet size so it looks neat. I need this asap. Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

夜吻♂芭芘 2024-09-01 17:17:30

您可以设置开关的框架。

将开关放置在警报视图的中间

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 之后放置开关。否则,纸张的大小为零。如果更换,可以在底部添加相同的空间。

希望这有帮助

You can set the frame of the switch.

To position the switch in the middle of the alert view

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;

It is important, that you position the switch after calling showInView for the sheet. Otherwise the size of the sheet is zero. If you replace you can add same space to the bottom.

Hope this helps

好久不见√ 2024-09-01 17:17:30

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

此外,您还会在同一线程中找到许多其他美化选项

I think you have the option to do it by modifying frame of UISwitch like trumi described above. If you want to place a UIButton with image them here is a undocumented option. Undocumented, so risk of rejection.

Also you will find many other beautification option in the same thread

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文