2016-05-22 10 views
0

我一直在为我正在构建的应用程序开发自定义工具栏。 我遇到了调整工具栏大小的问题,它正确调整了大型设备(如iPad或iPhone 6)的尺寸,但更具体的问题是定位工具栏中的按钮元素。objective-c iOS - 自定义工具栏没有按预期定位按钮

下面是在较小的设备上成功工作的问题图像。 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]; 

}

我试图调整子元素(按钮)autosizingmasks,但是这并没有在所有的证明是成功的,它使我相当卡住。 任何可能有助于确定问题的帮助将不胜感激。 谢谢。

回答

0

事实证明,我是在错误的地方寻找这个问题的答案。指定X,Y值的单独调用方法不正确,因此对自动调整掩码的任何更改都不会解决问题。

谢谢你花时间研究这个。

0

你试过吗?

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

我已经尝试过了,不幸的是,相机按钮的布局没有什么区别。 – Ashley