UISlider约束以编程方式:"约束项必须都是UIView或子类的实例"

时间:2015-03-06 17:52:24

标签: ios objective-c xcode uislider

我的约束导致我的应用程序崩溃,因为它们不是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"是这个类的一个强大的非原子属性,并在我的初始化器中实现,如下所示:

[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'

我已经尽力而为,没有运气。有任何想法吗? 提前谢谢!

1 个答案:

答案 0 :(得分:-3)

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

    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];