将cameraOverlayView约束到屏幕中心

时间:2013-11-12 00:55:15

标签: ios nslayoutconstraint autoresizingmask zbar-sdk camera-overlay

我正在创建一个iPad应用程序中使用zBAR QR Reader,并且我已经为ZBarReaderViewController添加了自定义十字准线覆盖。我设法将叠加层放到中心,但是当设备旋转时,叠加层显然偏离中心。

我已经搜索过以编程方式约束视图,但没有一个建议有效。我已经尝试了UIViewAutoResizingMask的所有可能组合,并且我尝试使用[NSLayoutConstraint constraintWithItem:attribute:relatedBy:toItem:attribute:multiplier:constant:]方法进行了大量不同的设置。

这是我的代码:

self.readerqr = [ZBarReaderViewController new];

int readerPointX = ((self.view.bounds.origin.x) + (self.view.bounds.size.width / 2.0));
int readerPointY = ((self.view.bounds.origin.y) + (self.view.bounds.size.height / 2.0) -60);

self.readerqr.readerDelegate = self;
self.readerqr.showsHelpOnFail = NO;

CGRect viewFrame = CGRectMake(readerPointX, readerPointY, 200, 200);

self.crossHair = [[UIImageView alloc] initWithFrame:viewFrame];

self.crossHair.image = [UIImage imageNamed:@"qr.png"];

[self.readerqr setCameraOverlayView:self.crossHair];

constraint = [NSLayoutConstraint constraintWithItem:self.crossHair
                                          attribute:NSLayoutAttributeHeight
                                          relatedBy:NSLayoutRelationEqual
                                             toItem:nil
                                          attribute:NSLayoutAttributeNotAnAttribute
                                         multiplier:1
                                           constant:200];

[self.crossHair addConstraint:constraint];

constraint = [NSLayoutConstraint constraintWithItem:self.crossHair
                                          attribute:NSLayoutAttributeWidth
                                          relatedBy:NSLayoutRelationEqual
                                             toItem:nil
                                          attribute:NSLayoutAttributeNotAnAttribute
                                         multiplier:1
                                           constant:200];
[self.crossHair addConstraint: constraint];

设置此选项的正确方法是什么,无论方向如何变化,我的叠加层始终位于屏幕中央?任何帮助都会很棒,谢谢。

1 个答案:

答案 0 :(得分:0)

此问题可能不再适用,但仍希望此代码可以帮助某人。 首先,使用自动布局时,您不需要处理帧。 其次,试试这段代码

self.readerqr = [ZBarReaderViewController new];

self.readerqr.readerDelegate = self;
self.readerqr.showsHelpOnFail = NO;

self.crossHair = [[UIImageView alloc] init];
self.crossHair.translatesAutoresizingMaskIntoConstraints = NO;
self.crossHair.image = [UIImage imageNamed:@"qr.png"];

[self.readerqr setCameraOverlayView:self.crossHair];

[self.readerqr addConstraint:[NSLayoutConstraint constraintWithItem:self.crossHair
                                                          attribute:NSLayoutAttributeHeight
                                                          relatedBy:NSLayoutRelationEqual
                                                             toItem:nil
                                                          attribute:NSLayoutAttributeNotAnAttribute
                                                         multiplier:1
                                                           constant:200]];

[self.readerqr addConstraint: [NSLayoutConstraint constraintWithItem:self.crossHair
                                                           attribute:NSLayoutAttributeWidth
                                                           relatedBy:NSLayoutRelationEqual
                                                              toItem:nil
                                                           attribute:NSLayoutAttributeNotAnAttribute
                                                          multiplier:1
                                                            constant:200]];

[self.readerqr addConstraint:[NSLayoutConstraint constraintWithItem:self.crossHair
                                                          attribute:NSLayoutAttributeCenterX
                                                          relatedBy:NSLayoutRelationEqual
                                                             toItem:self.readerqr
                                                          attribute:NSLayoutAttributeCenterX
                                                         multiplier:1
                                                           constant:0]];
[self.readerqr addConstraint:[NSLayoutConstraint constraintWithItem:self.crossHair
                                                          attribute:NSLayoutAttributeCenterY
                                                          relatedBy:NSLayoutRelationEqual
                                                             toItem:self.readerqr
                                                          attribute:NSLayoutAttributeCenterY
                                                         multiplier:1
                                                           constant:0]];