gpt4 book ai didi

ios - 将外部 UI 组件添加到 Storyboard

转载 作者:行者123 更新时间:2023-11-28 19:49:36 27 4
gpt4 key购买 nike

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

最佳答案

如果您查看代码和 AYVibrantButton 的自述文件,它说您应该将按钮添加到 UIVisualEffectView,并且必须使用initWithFrame:style:。当你向 Storyboard添加一个按钮并将其类设置为 AYVibrantButton 时,调用的 init 方法将是 initWithCoder:,因此作者在 中所做的设置都没有>initWithFrame:style: 发生。如果要在 Storyboard中添加按钮,则需要更新代码以包含 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];
}

关于ios - 将外部 UI 组件添加到 Storyboard,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30085951/

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