简体   繁体   中英

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

I have been working on a custom toolbar for an app which I am building. I have hit a problem with resizing of the toolbar, it resizes on larger devices such as an iPad or iPhone 6 correctly, but more specifically the issue is with the positioning of a button element which is in the Toolbar.

Below is an image of the issue working successfully on a smaller device. Working correctly on smaller device

And here is the issue on an iPad: Incorrect Camera Position

As you can see from the screenshots, the camera button position is not as expected or as desired.

The following is code which sets up the toolbar elements:

-(void)setupToolbar:(NSString *)buttonLabel

{ self.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleTopMargin; // ADDED THE CONSTRAINT TO PLACE AT THE BOTTOM 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];

}

I have tried adjusting the child elements (buttons) autosizingmasks, but that has not proven successful at all and it has led me to be rather stuck. Any help which could help identify what the problem is would be gratefully appreciated. Thanks.

你有尝试过吗?

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

It turns out I was looking in the wrong place for the answer to this problem. A separate calling method which specifies an X,Y value was incorrect so any changes to the autosizingmask was never going to solve the problem.

Thank you for any time spent looking into this.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM