gpt4 book ai didi

iOS:动态显示/隐藏 UI 元素

转载 作者:行者123 更新时间:2023-11-29 01:48:35 24 4
gpt4 key购买 nike

我有一个应用程序,其结果的 UITableView 位于中心列,顶部有一个小搜索栏。我想动态添加/删除一个显示“重置搜索”的按钮并将其固定到 View 顶部。

有几种方法可以解决这个问题,我担心它们对我来说看起来都很丑陋或很老套。即:

  • 在 Storyboard编辑器中添加按钮,并在代码中显示/隐藏它。问题是我已经在 Storyboard中以这种方式指定了一堆 View ,因此定位/选择它们是一个巨大的痛苦,因为它们彼此重叠。

  • 在代码中添加按钮。但现在我的 UI 在两个地方指定: Storyboard中的内容以及代码中发生的其他修改。

做这样的事情的标准方法是什么?当我有按钮/对话框/等时,如何防止我的 Storyboard变得一团糟。需要动态显示/隐藏?

最佳答案

嗯,我的第一个答案是首先不要使用 Storyboard。不过,据我所知,这对这种情况没有帮助。

如果我是你,我会选择选项 2。它是一个单独的按钮,它有一个特定的用例。在代码中指定它并没有什么坏处。以下为

.h
@property (nonatomic, strong) UIButton *resetButton;

.m
//I'm guessing you're using a VC, so I'd put this in viewDidLoad

self.resetButton = [[UIButton alloc]initWithFrame:YOUR FRAME];
self.resetButton.alpha = 0.0;
//any other styling
[self.view addSubview:self.resetButton];
self.resetButton addTarget:self action:@selector(onReset) forControlEvents:UIControlEventTouchUpInside];

//and then add these three methods

- (void)onReset {
//called when reset button is tapped
}

- (void)showResetButton {
[UIView animateWithDuration:.3 animations:^{
self.resetButton.alpha = 1.0;
}];
}

- (void)hideResetButton {
[UIView animateWithDuration:.3 animations:^{
self.resetButton.alpha = 0.0;
}];
}

关于iOS:动态显示/隐藏 UI 元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31658802/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com