制作自定义 UISegmentControl(某种程度)
我有这个模型:
正如您所看到的,这是一种导航菜单。它的功能应该与分段控件相同,我将根据活动项目更改 tableView。
实现这个最简单的方法是什么?
我已经开始创建我的 UIView 子类,但发现我必须创建一个委托,监视点击事件和其他内容。 这是最好的方法吗? 我应该子类化 UISegmentedControl 吗?
还有其他建议吗?
请为我指出正确的方向。我对 Obj-c 充满信心,但是制作这些东西让我的想法变得疯狂。
I've got this mock up:
As you can see, it's a sort of navigation-menu. It's functionality should be the same as a segmented control, and i am going to change the tableView based on the item active.
What would be the easiest way to implement this?
I have started makin my UIView-subclass, but found out that i had to then make a delegate, watch for tap-events and stuff.
Is this the best way to do it? Should i subclass UISegmentedControl?
Any other advice?
Please, point me in the right direction. I feel confident in Obj-c, but making these kinds of stuff makes my mind goes crazy.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
从概念上讲,
UISegmentedControl
似乎是一个不错的选择,但我认为它不够灵活,无法创建您想要的效果。您是否考虑过将三个
UIButton
控件放入自定义视图中?您可以使用setBackgroundImage:forState:
自定义每个按钮的图像,以获取模型中的边框样式。将按下的按钮的selected
属性设置为YES
,UIButton
将为您处理绘图。您可以设置一个操作方法来通过调用来检测按下了哪个按钮。
委托是符合您创建的协议的任何类。因此,您可以在标头中创建一个委托协议,如下所示:
委托只是您的
MyControl
类中的一个属性:在您的按钮按下处理程序中,例如:
现在,您只需拥有包含您的控件的视图控制器采用协议:
并实现方法:
Conceptually,
UISegmentedControl
seems like a good choice for this, but I don't think it's quite flexible enough to create the effect you're going for here.Have you considered putting three
UIButton
controls inside a custom view? You can customize the images for each button usingsetBackgroundImage:forState:
to get the border style in your mockup. Set theselected
property of the pressed button toYES
, andUIButton
will handle the drawing for you.You can set up an action method to detect which button was pressed by calling
A delegate is just any class that conforms to a protocol you create. So you would create a delegate protocol in your header like this:
And the delegate is just a property in your
MyControl
class:And in your button press handlers, for example:
Now, you just have to have the view controller that contains your control adopt the protocol:
And implement the method: