简体   繁体   English

UIBarButtonItem动作选择器ios11

[英]UIBarButtonItem action selector ios11

UIButton *rideButton = [UIButton buttonWithType:UIButtonTypeCustom];
rideButton.titleLabel.font = CUSTOM_BUTTON_FONT;
[rideButton setTitleColor:[UIColor CUSTOM_BUTTON_TITLE_COLOR1] forState:UIControlStateNormal];
[rideButton setTitleColor:[UIColor CUSTOM_BUTTON_TITLE_COLOR2] forState:UIControlStateHighlighted];
[rideButton setTitle:@"Ride" forState:UIControlStateNormal];
[rideButton addTarget:self action:@selector(rideButtonClicked) forControlEvents:UIControlEventTouchUpInside];
CGSize s1 = [rideButton.titleLabel.text sizeWithFont:rideButton.titleLabel.font];
rideButton.frame = (CGRect) {
    .size.width = (s1.width+rideBarButtonGap)>100?100:(s1.width+rideBarButtonGap),
        .size.height = s1.height + rideBarButtonGap
};

rideButton.backgroundColor = [UIColor redColor];
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 11.0)
{
    [[rideButton.widthAnchor constraintEqualToConstant:rideButton.frame.size.width] setActive:YES];
    [[rideButton.heightAnchor constraintEqualToConstant:rideButton.frame.size.height] setActive:YES];
}

UIBarButtonItem *rideBarButton= [[UIBarButtonItem alloc] initWithCustomView:rideButton];
self.navigationItem.rightBarButtonItem = rideBarButton;

This is my code to create custom UIBarButtonItem. 这是我创建自定义UIBarButtonItem的代码。 I have set the background color to red just to see the bounds of the button. 我已将背景色设置为红色,只是为了查看按钮的边界。 The background color looks perfectly outside the text but I'm unable to click the button properly. 背景颜色在文本外部看起来很完美,但是我无法正确单击按钮。 The button's action get fired only when 'Ri' is selected. 仅当选择“ Ri”时,按钮的动作才会触发。 If 'de' is selected, the function is not called. 如果选择“ de”,则不调用该函数。 It happens only in iOS 11. I have added width and height constraints to fix this but there is no change. 它仅在iOS 11中发生。我添加了宽度和高度约束来解决此问题,但没有任何更改。

EDIT: When I increase the width of widthAnchor, I'm able to acheive what I want but the button moves left and theres lot of gap between the button and Screen. 编辑:当我增加widthAnchor的宽度时,我能够达到我想要的,但是按钮向左移动,并且按钮和屏幕之间有很多间隙。 So I tried to align the content using rideButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight; 所以我尝试使用rideButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;来对齐内容rideButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight; . Though the content moves right, the clickable area is still in the middle. 尽管内容向右移动,但可单击区域仍在中间。

Try this: 尝试这个:

public extension UIView {
//From iOS 11 -> UIBarButtonItem uses autolayout instead of dealing with frames. so adding contraint for the barbuttonItem
public func applyNavBarConstraints(size: (width: CGFloat, height: CGFloat)) {
    if #available(iOS 11.0, *) {
        let widthConstraint = self.widthAnchor.constraint(equalToConstant: size.width)
        let heightConstraint = self.heightAnchor.constraint(equalToConstant: size.height)
        heightConstraint.isActive = true
        widthConstraint.isActive = true
    }
}}

and use it as below: 并如下使用:

rideBarButton.applyNavBarConstraints(size: (width: 50, height: 44)) //expected height and width

This works for me. 这对我有用。 Hope this helps! 希望这可以帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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