ios - Storyboard中的自定义 UISegmentedControl
<p><p>我正在尝试创建一个自定义 UISegmentedControl(类似于 <a href="https://github.com/kferrell/KFSegmentedControlExample" rel="noreferrer noopener nofollow">this one</a> )并希望在我的 Storyboard中展示它的设计。</p>
<p>我创建了这个子类:</p>
<p>CustomSegmentedControl.h</p>
<pre><code>IB_DESIGNABLE
@interface CustomSegmentedControl : UISegmentedControl
@end
</code></pre>
<p>CustomSegmentedControl.m</p>
<pre><code>#import "CustomSegmentedControl.h"
@implementation CustomSegmentedControl
- (id)initWithFrame:(CGRect)frame {
self = ;
if (self) {
;
}
return self;
}
- (id)initWithCoder:(NSCoder *)aDecoder {
self = ;
if (self) {
;
}
return self;
}
- (void)commonInit {
UIImage *backgroundOff = ;
UIImage *backgroundOn = ;
UIImage *separator = ;
CGRect frame = CGRectMake(self.frame.origin.x,
self.frame.origin.y,
self.frame.size.width,
50.0);
;
[self setBackgroundImage:backgroundOff
forState:UIControlStateNormal
barMetrics:UIBarMetricsDefault];
[self setBackgroundImage:backgroundOn
forState:UIControlStateSelected
barMetrics:UIBarMetricsDefault];
[self setDividerImage:separator
forLeftSegmentState:UIControlStateNormal
rightSegmentState:UIControlStateNormal
barMetrics:UIBarMetricsDefault];
[self setDividerImage:separator
forLeftSegmentState:UIControlStateSelected
rightSegmentState:UIControlStateNormal
barMetrics:UIBarMetricsDefault];
[self setDividerImage:separator
forLeftSegmentState:UIControlStateNormal
rightSegmentState:UIControlStateSelected
barMetrics:UIBarMetricsDefault];
}
@end
</code></pre>
<p>我在 Storyboard中将 SegmentedControl 的类更改为 CustomSegmentedControl,当我启动应用程序时,控件看起来应该是这样。但在我的 Storyboard中,UISegmentedControl 的外观是常规的。</p>
<p>有没有与它在 Storyboard中的真实外观相匹配?</p>
<p>谢谢</p></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>好的,这就是你的做法。首先,您使用 <code>IB_DESIGNABLE</code> 走在正确的轨道上。 </p>
<p>第二件事,您需要为要在 Storyboard 中控制和渲染的事物创建 <code>IB_INSPECTABLE</code> 属性,并在 .m 文件中为它们创 build 置器。 </p>
<p>我所做的只是一个开始,我相信这里还有更多可以做的。 </p>
<p>附上代码供大家引用:</p>
<p>.h 文件:</p>
<pre><code>IB_DESIGNABLE
@interface CustomSegmentedControl : UISegmentedControl
@property (nonatomic) IBInspectable UIImage *backgroundOff;
@property (nonatomic) IBInspectable UIImage *backgroundOn;
@property (nonatomic) IBInspectable UIImage *separator;
@end
</code></pre>
<p>.m 文件:</p>
<pre><code>#import "CustomSegmentedControl.h"
@implementation CustomSegmentedControl
- (id)initWithFrame:(CGRect)frame {
self = ;
if (self) {
;
}
return self;
}
- (id)initWithCoder:(NSCoder *)aDecoder {
self = ;
if (self) {
;
}
return self;
}
-(void) setBackgroundOff:(UIImage *)backgroundOff
{
_backgroundOff = backgroundOff;
[self setBackgroundImage:self.backgroundOff
forState:UIControlStateNormal
barMetrics:UIBarMetricsDefault];
}
-(void) setBackgroundOn:(UIImage *)backgroundOn
{
_backgroundOn = backgroundOn;
[self setBackgroundImage:self.backgroundOn
forState:UIControlStateSelected
barMetrics:UIBarMetricsDefault];
}
-(void) setSeparator:(UIImage *)separator
{
_separator = separator;
[self setDividerImage:self.separator
forLeftSegmentState:UIControlStateNormal
rightSegmentState:UIControlStateNormal
barMetrics:UIBarMetricsDefault];
[self setDividerImage:self.separator
forLeftSegmentState:UIControlStateSelected
rightSegmentState:UIControlStateNormal
barMetrics:UIBarMetricsDefault];
[self setDividerImage:self.separator
forLeftSegmentState:UIControlStateNormal
rightSegmentState:UIControlStateSelected
barMetrics:UIBarMetricsDefault];
}
- (void)commonInit {
_backgroundOff = ;
_backgroundOn = ;
_separator = ;
CGRect frame = CGRectMake(self.frame.origin.x,
self.frame.origin.y,
self.frame.size.width,
50.0);
;
[self setBackgroundImage:self.backgroundOff
forState:UIControlStateNormal
barMetrics:UIBarMetricsDefault];
[self setBackgroundImage:self.backgroundOn
forState:UIControlStateSelected
barMetrics:UIBarMetricsDefault];
[self setDividerImage:self.separator
forLeftSegmentState:UIControlStateNormal
rightSegmentState:UIControlStateNormal
barMetrics:UIBarMetricsDefault];
[self setDividerImage:self.separator
forLeftSegmentState:UIControlStateSelected
rightSegmentState:UIControlStateNormal
barMetrics:UIBarMetricsDefault];
[self setDividerImage:self.separator
forLeftSegmentState:UIControlStateNormal
rightSegmentState:UIControlStateSelected
barMetrics:UIBarMetricsDefault];
}
@end
</code></pre>
<p>这是它在界面生成器中的外观:</p>
<p> <a href="/image/OaFNe.png" rel="noreferrer noopener nofollow"><img src="/image/OaFNe.png" alt="enter image description here"/></a> </p>
<p>该组件现在看起来更接近实际实现。 </p>
<p>您现在可以看到,属性检查器面板中有 3 个新的可编辑属性。我希望这是一个好的开始,你可以从这里开始:)</p>
<p>希望这会有所帮助。 </p></p>
<p style="font-size: 20px;">关于ios -Storyboard中的自定义 UISegmentedControl,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/31872046/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/31872046/
</a>
</p>
页:
[1]