UISlider 以编程方式约束:"Constraint items must each be an instance of UIView or subclass"

UISlider Constraints Programmatically: "Constraint items must each be an instance of UIView or subclass"

我的约束导致我的应用程序崩溃,因为它们不是 UIView 的实例。但是,至少据我所知,它们 UIView 的实例。我的目标是将此滑块作为 UIBarbuttonItem 附加到工具栏。这是我在 viewDidLoad 中的内容:

// Slider Implementation
slider_textSize.minimumValue = 9;
slider_textSize.maximumValue = 72;
slider_textSize.value = 12;
slider_textSize.continuous = YES;

[self.view setUserInteractionEnabled:YES];
[self.view addSubview:self.slider_textSize];

self.slider_textSize.translatesAutoresizingMaskIntoConstraints = NO;

[self.view addConstraint:[NSLayoutConstraint
                          constraintWithItem:self.slider_textSize
                          attribute:NSLayoutAttributeLeft
                          relatedBy:NSLayoutRelationEqual
                          toItem:self.view
                          attribute:NSLayoutAttributeLeft
                          multiplier:1
                          constant:5]];

[self.view addConstraint:[NSLayoutConstraint
                          constraintWithItem:self.slider_textSize
                          attribute:NSLayoutAttributeRight
                          relatedBy:NSLayoutRelationEqual
                          toItem:button_done attribute:NSLayoutAttributeRight
                          multiplier:1
                          constant:10]];

"slider_textSize" 是此 class 的强非原子 属性 并在我的初始化程序中实现如下:

[slider_textSize addTarget:self
                 action:@selector(slider_textSizeValueChanged:)
                 forControlEvents:UIControlEventValueChanged];

我一直在使用 this 问题的答案,但它似乎对我不起作用。这正是我得到的错误:

2015-03-06 10:26:08.300 rED[14238:3168995] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** +[NSLayoutConstraint constraintWithItem:attribute:relatedBy:toItem:attribute:multiplier:constant:]: Constraint items must each be an instance of UIView or subclass'

我已经尽我所能,但没有成功。有任何想法吗? 提前致谢!

我最终放弃了约束,因为它们对于条形按钮项目变得太复杂了。相反,我使滑块的宽度与屏幕的宽度成比例。

    CGRect screenRect = [[UIScreen mainScreen] bounds];
    CGFloat screenWidth = screenRect.size.width;
    CGFloat screenHeight = screenRect.size.height;

    CGFloat sliderWidth = .786 * screenWidth;
    CGRect frame = CGRectMake(0, 0, sliderWidth, 32);
    slider_textSize = [[UISlider alloc] initWithFrame:frame];