Swift 在屏幕顶部添加按钮的最佳方法是什么
What is the best approach to add buttons on top of the screen in Swift
我正在尝试制作一个读取日历事件的应用程序,并在 Swift 中制作一个会议列表。我需要让它看起来像这样:
顶部始终可见。该列表将是可滚动的,当用户在不同的日期点击时,列表将加载不同的数据。当用户滚动到一天结束并继续滚动时,顶部栏中的天数也需要调整(天数必须进一步移动)。
我尝试使用带有导航栏的导航控制器来执行此操作。
这是正确的做法吗?
But is this the right approach
如果你的意思是"Can the days be in the navigation bar",那么不是。这不是正确的做法。
一个不错的方法是只使用一个单独的 "container" 视图,它包含按钮,并且滚动视图与此 "container" 处于同一级别。这样视图将在顶部不会滚动。
关于基于滚动更改顶部按钮 - 您可以使用 UIScrollViewDelegate
来获得准确的位置。由于我不知道确切的情况,我无法建议根据滚动位置确定日期的最佳方法是什么。
如果我没理解错的话,您只需要实现一个水平滚动的集合视图即可。 Table 每次点击集合视图单元格(didSelectItemAtIndexPath 方法)时,视图都会重新加载。
干杯。
我只是设置 scrollview.frame 以适应视图控制器顶部的按钮高度。确保如果您使用故事板,则滚动视图顶部没有按钮。
let width = self.view.frame.size.width
button1.frame = CGRectMake(0, 0, width / 4, 40)
button2.frame = CGRectMake(width / 4, 0, width / 4, 40)
button3.frame = CGRectMake((width / 4) * 2, 0, width / 4, 40)
button4.frame = CGRectMake((width / 4) * 3, 0, width / 4, 40)
button5.frame = CGRectMake(0, 41, width / 4, 40)
button6.frame = CGRectMake(width / 4, 41, width / 4, 40)
button7.frame = CGRectMake((width / 4) * 2, 41, width / 4, 40)
button8.frame = CGRectMake((width / 4) * 3, 41, width / 4, 40)
// scrollview frame size
scrollView.frame = CGRectMake(0, 80, self.view.frame.width, self.view.frame.height)
scrollView.contentSize.height = self.view.frame.height
scrollViewHeight = scrollView.frame.size.height
我正在尝试制作一个读取日历事件的应用程序,并在 Swift 中制作一个会议列表。我需要让它看起来像这样:
顶部始终可见。该列表将是可滚动的,当用户在不同的日期点击时,列表将加载不同的数据。当用户滚动到一天结束并继续滚动时,顶部栏中的天数也需要调整(天数必须进一步移动)。 我尝试使用带有导航栏的导航控制器来执行此操作。
这是正确的做法吗?
But is this the right approach
如果你的意思是"Can the days be in the navigation bar",那么不是。这不是正确的做法。
一个不错的方法是只使用一个单独的 "container" 视图,它包含按钮,并且滚动视图与此 "container" 处于同一级别。这样视图将在顶部不会滚动。
关于基于滚动更改顶部按钮 - 您可以使用 UIScrollViewDelegate
来获得准确的位置。由于我不知道确切的情况,我无法建议根据滚动位置确定日期的最佳方法是什么。
如果我没理解错的话,您只需要实现一个水平滚动的集合视图即可。 Table 每次点击集合视图单元格(didSelectItemAtIndexPath 方法)时,视图都会重新加载。 干杯。
我只是设置 scrollview.frame 以适应视图控制器顶部的按钮高度。确保如果您使用故事板,则滚动视图顶部没有按钮。
let width = self.view.frame.size.width
button1.frame = CGRectMake(0, 0, width / 4, 40)
button2.frame = CGRectMake(width / 4, 0, width / 4, 40)
button3.frame = CGRectMake((width / 4) * 2, 0, width / 4, 40)
button4.frame = CGRectMake((width / 4) * 3, 0, width / 4, 40)
button5.frame = CGRectMake(0, 41, width / 4, 40)
button6.frame = CGRectMake(width / 4, 41, width / 4, 40)
button7.frame = CGRectMake((width / 4) * 2, 41, width / 4, 40)
button8.frame = CGRectMake((width / 4) * 3, 41, width / 4, 40)
// scrollview frame size
scrollView.frame = CGRectMake(0, 80, self.view.frame.width, self.view.frame.height)
scrollView.contentSize.height = self.view.frame.height
scrollViewHeight = scrollView.frame.size.height