objective-c iOS - 自定义工具栏未按预期定位按钮

objective-c iOS - Custom Toolbar not positioning button as expected

我一直在为我正在构建的应用程序开发自定义工具栏。 我遇到了调整工具栏大小的问题,它在较大的设备(例如 iPad 或 iPhone 6 上正确调整大小,但更具体地说,问题在于按钮元素的定位工具栏。

下面是在较小的设备上成功解决问题的图片。 Working correctly on smaller device

这是 iPad 上的问题: Incorrect Camera Position

正如您从屏幕截图中看到的那样,相机按钮位置不符合预期或不符合要求。

以下是设置工具栏元素的代码:

-(void)setupToolbar:(NSString *)buttonLabel

{ self.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleTopMargin; // 添加约束以放置在底部 self.tintColor = [UIColor lightGrayColor];

/* Create custom send button*/
UIImage *buttonImage = [UIImage imageNamed:@"send.png"];
buttonImage          = [buttonImage stretchableImageWithLeftCapWidth:floorf(buttonImage.size.width/2) topCapHeight:floorf(buttonImage.size.height/2)];

UIButton *button               = [UIButton buttonWithType:UIButtonTypeCustom];
button.titleLabel.font         = [UIFont boldSystemFontOfSize:15.0f];
button.titleLabel.shadowOffset = CGSizeMake(0, -1);
button.titleEdgeInsets         = UIEdgeInsetsMake(0, 2, 0, 2);
button.contentStretch          = CGRectMake(0.5, 0.5, 0, 0);
button.contentMode             = UIViewContentModeScaleToFill;

[button setBackgroundImage:buttonImage forState:UIControlStateNormal];
[button setTitle:buttonLabel forState:UIControlStateNormal];
[button addTarget:self action:@selector(inputButtonPressed) forControlEvents:UIControlEventTouchDown];
[button sizeToFit];

self.inputButton = [[UIBarButtonItem alloc] initWithCustomView:button];
self.inputButton.customView.autoresizingMask = UIViewAutoresizingFlexibleTopMargin;
/* Disable button initially */
self.inputButton.enabled = NO;

//Camera
UIImage *buttonImageCam = [UIImage imageNamed:@"btnCamera.png"];
buttonImageCam          = [buttonImageCam stretchableImageWithLeftCapWidth:floorf(buttonImageCam.size.width/2) topCapHeight:floorf(buttonImageCam.size.height/2)];

UIButton *btnCam               = [UIButton buttonWithType:UIButtonTypeCustom];
btnCam.titleLabel.font         = [UIFont boldSystemFontOfSize:15.0f];
btnCam.titleLabel.shadowOffset = CGSizeMake(0, -1);
btnCam.titleEdgeInsets         = UIEdgeInsetsMake(0, 2, 0, 2);
btnCam.contentStretch          = CGRectMake(0.5, 0.5, 0, 0);
btnCam.contentMode             = UIViewContentModeScaleToFill;

[btnCam setBackgroundImage:buttonImageCam forState:UIControlStateNormal];
[btnCam setTitle:buttonLabel forState:UIControlStateNormal];
[btnCam addTarget:self action:@selector(inputCamButtonPressed) forControlEvents:UIControlEventTouchDown];
[btnCam sizeToFit];

self.inputButtonCam = [[UIBarButtonItem alloc] initWithCustomView:btnCam];
self.inputButtonCam.customView.autoresizingMask = UIViewAutoresizingFlexibleTopMargin;
/* Disable button initially */
self.inputButtonCam.enabled = YES;

/* Create UIExpandingTextView input */
self.textView = [[BHExpandingTextView alloc] initWithFrame:CGRectMake(40, 7, self.bounds.size.width - 70, 26)];
self.textView.internalTextView.scrollIndicatorInsets = UIEdgeInsetsMake(4.0f, 0.0f, 10.0f, 0.0f);
self.textView.autoresizingMask = UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleWidth;
self.textView.delegate = self;
[self addSubview:self.textView];

/* Right align the toolbar button */
UIBarButtonItem *flexItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];

NSArray *items = [NSArray arrayWithObjects: flexItem, self.inputButton,self.inputButtonCam, nil];
[self setItems:items animated:NO];

}

我曾尝试调整子元素(按钮)自动调整大小掩码,但事实证明这根本没有成功,这让我陷入了困境。 任何有助于确定问题所在的帮助将不胜感激。 谢谢。

你试过了吗?

self.inputButtonCam.customView.autoresizingMask = UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleTopMargin;

原来我在错误的地方寻找这个问题的答案。指定 X、Y 值的单独调用方法不正确,因此对 autosizingmask 的任何更改都不会解决问题。

感谢您花时间研究这个问题。