2

我在我正在创建的 iPad 应用程序中使用 zBAR QR 阅读器,并在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];

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

4

1 回答 1

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]];
于 2015-07-04T09:01:04.203 回答