ios - 将 cameraOverlayView 约束到屏幕中心

标签 ios nslayoutconstraint autoresizingmask zbar-sdk camera-overlay

我正在创建的 iPad 应用程序中使用 zBAR QR 阅读器,并且我已将自定义十字准线覆盖添加到 ZBarReaderViewController .我已经设法将覆盖层置于中心,但是当设备旋转时,覆盖层显然会偏离中心。

我已经搜索了有关以编程方式限制 View 的方法,但没有任何建议有效。我已经尝试了所有可能的组合 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];

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

最佳答案

这个问题可能不再相关,但仍然希望这段代码对某人有所帮助。
首先,使用自动布局时,您不需要处理帧。
其次,试试这段代码

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

关于ios - 将 cameraOverlayView 约束到屏幕中心,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19918978/

相关文章:

iphone - Storyboard警告 : prototype table cells must have reuse identifiers

ios - 平移手势干扰滚动

ios - 在横向模式下,背景颜色仅出现在屏幕的一半上

ios - 自动布局、尺寸类别和字体大小

animation - 处理 iOS 10 中的 AutoLayout 约束动画差异?

ios - 可以将自动调整大小掩码与自动布局混合使用吗?

ios - 仅在 iOS 7 上向 UITableView 添加 subview 时出现 "Auto Layout still required"错误

iphone - 如何找出 UIViewController View 的确切框架?

ios - 使用 for-in 循环枚举

iOS 11 : "Unable to simultaneously satisfy constraints"