【问题标题】:How to achieve this layout on buttons in Storyboard?如何在 Storyboard 中的按钮上实现这种布局?
【发布时间】:2015-11-26 13:46:43
【问题描述】:

我已经在 Photoshop 中完成了我的应用程序设计,我目前正在 XCode 中编写应用程序。几个小时以来,我一直在摸索如何以“不错”的方式制作下面的设计。我认为有一种简单的方法可以使UISegmentedControl 如下图所示,但您似乎无法将图像和文本添加到UISegmentedControl 上的按钮。我尝试了其他几种方法,例如使用标签、图像视图和在这些图层上方透明的按钮,但它看起来很hackish。我怎样才能用方角和下面的边框来实现这种布局?

【问题讨论】:

标签: ios swift storyboard ios9


【解决方案1】:

为了使用 UISegmentedControl 执行此操作,我认为您必须创建一个自定义 UISegmentedControl 类并以编程方式将您的自定义视图添加到每个段中。

另一种更好的方法可能是将 UICollectionView 与自定义单元格一起使用?

【讨论】:

  • 是的,我想过,但我不确定。我想这是最好的方法。
【解决方案2】:

正如有人已经提到的那样,可能最好的解决方法(如果您想要完全按照您的设计)是编写您自己的自定义控件。我前段时间做过类似的事情,这里是其中的一部分:

- (id)initInsideFrame:(CGRect)frame withSegments:(NSArray*)inputArray
{
    self = [super initWithFrame:frame];

    if (self) {
        [self configureDefaultAppearanceInFrame:frame];
        [self initSegmentsFromArray:inputArray];
        [self selectSegmentAtIndex:0];
    }

    return self;
}


- (void)initSegmentsFromArray:(NSArray*)inputArray isSimpleSwitch:(bool)isSimpleSwitch
{
    [self.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];

    _segmentsNumber = (int)inputArray.count;
    _selectedSegmentIndex = 0;
    _segmentsArray = [[NSMutableArray alloc] init];
    _titlesArray = [[NSMutableArray alloc] initWithArray:inputArray];

    for (int i = 0; i < _segmentsNumber; i++) {
        CustomSegment* segment = [[CustomSegment alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width / _segmentsNumber, self.frame.size.height)];

        segment.center = CGPointMake(self.frame.size.width * (2 * i + 1) / (_segmentsNumber * 2), self.frame.size.height / 2);
        segment.index = i;

        segment.title = [[UILabel alloc] initWithFrame:segment.frame];
        segment.title.center = CGPointMake(segment.frame.size.width / 2, segment.frame.size.height / 2);
        segment.title.textAlignment = NSTextAlignmentCenter;
        [segment addSubview:segment.title];

        [segment addTapGestureRecognition];
        segment.delegate = self;

        [self addSubview:segment];
        [_segmentsArray addObject:segment];
    }

    [self refreshInterface];
}

CustomSegment 类只是一个带有手势识别器的 UIView,以及一个允许识别被点击的段的委托。在你的情况下,你可以在里面放一些额外的图形、标签。

可悲的现实是,如果您想要一些漂亮的自定义界面,您可能必须自己编写。

【讨论】:

    猜你喜欢
    • 2013-11-16
    • 1970-01-01
    • 2013-09-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-25
    相关资源
    最近更新 更多