将外部UI组件添加到Storyboard

时间:2015-05-06 19:34:30

标签: ios objective-c xcode

我想在Xcode 6中为故事板添加一个外部UI组件(例如,来自https://github.com/a1anyip/AYVibrantButton的自定义按钮)。是否可以通过拖放直接将其添加到故事板?如果没有,我应该在哪里调用init函数,如何在代码中指定它的位置?非常感谢。

2 个答案:

答案 0 :(得分:2)

如果您查看代码和AYVibrantButton的自述文件,则表示您应该将按钮添加到UIVisualEffectView,并且必须使用initWithFrame:style:进行实例化。当您向故事板添加按钮并将其类设置为AYVibrantButton时,调用的init方法将为initWithCoder:,因此作者在initWithFrame:style:中所做的任何设置都不会发生。如果要在故事板中添加按钮,则需要更新代码以包含initWithCoder:方法。使用以下内容替换作者代码中的initWithFrame:style:实现

- (instancetype)initWithFrame:(CGRect)frame style:(AYVibrantButtonStyle)style {
    if (self = [super initWithFrame:frame]) {
        self.style = style;
        [self commonSetup];
    }
    return self;
}


-(instancetype)initWithCoder:(NSCoder *)aDecoder {
    if (self = [super initWithCoder:aDecoder]) {
        self.style = AYVibrantButtonStyleFill; // need to add add a style here. Change it to one of the other styles to match your needs
        [self commonSetup];
    }
    return self;
}


-(void)commonSetup {
    self.opaque = NO;
    self.userInteractionEnabled = YES;

    // default values
    _animated = YES;
    _animationDuration = kAYVibrantButtonDefaultAnimationDuration;
    _cornerRadius = kAYVibrantButtonDefaultCornerRadius;
    _roundingCorners = kAYVibrantButtonDefaultRoundingCorners;
    _borderWidth = kAYVibrantButtonDefaultBorderWidth;
    _translucencyAlphaNormal = kAYVibrantButtonDefaultTranslucencyAlphaNormal;
    _translucencyAlphaHighlighted = kAYVibrantButtonDefaultTranslucencyAlphaHighlighted;
    _alpha = kAYVibrantButtonDefaultAlpha;
    _activeTouch = NO;

    // create overlay views
    [self createOverlays];

#ifdef __IPHONE_8_0
    // add the default vibrancy effect
    self.vibrancyEffect = [UIVibrancyEffect effectForBlurEffect:[UIBlurEffect effectWithStyle:UIBlurEffectStyleLight]];
#endif

    [self addTarget:self action:@selector(touchDown) forControlEvents:UIControlEventTouchDown | UIControlEventTouchDragInside];
    [self addTarget:self action:@selector(touchUp) forControlEvents:UIControlEventTouchUpInside | UIControlEventTouchUpOutside | UIControlEventTouchDragOutside | UIControlEventTouchCancel];
}

答案 1 :(得分:1)

该对象是UIButton的子类。因此,假设.h / .m文件位于项目中,只需将UIButton添加到故事板,转到身份检查器,然后将类从UIButton更改为AYVibrantButton。如果您需要对它做任何特殊的事情,只需创建一个插座即可。