2016-05-22 75 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

相關問題