簡體   English   中英

iOS 11導航欄按鈕項目

[英]IOS 11 Navigation Bar Button Item

我們使用下面的代碼在IOS 10 SDK和XCode 8的導航欄中顯示leftbarbutton項,但是當我們更新為xcode9和ios 11 sdk時。 左欄按鈕項不顯示。 我在網上搜索,但找不到任何解決方案。 有人知道答案嗎?

UIBarButtonItem *cancelButton = nil;

if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"11")) {
    UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 35, 35)];
    //button.imageEdgeInsets = UIEdgeInsetsMake(0, -15, 0, 15);
    //button.contentEdgeInsets = UIEdgeInsetsMake(13, 8, 13, 8);
    NSLayoutConstraint *widthConst = [button.widthAnchor constraintEqualToConstant:32.0];
    NSLayoutConstraint *heightConst = [button.heightAnchor constraintEqualToConstant:32.0];
    [button setImage:[UIImage imageNamed:@"cross"] forState:UIControlStateNormal];
    [button addTarget:self action:@selector(hide) forControlEvents:UIControlEventTouchUpInside];
    cancelButton = [[UIBarButtonItem alloc] initWithCustomView:button];

    widthConst.active = YES;
    heightConst.active = YES;

} else {
    cancelButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"cross"] style:UIBarButtonItemStylePlain target:loginRouter action:@selector(hide)];

}

cancelButton.tintColor = [UIColor whiteColor];
loginViewController.navigationItem.leftBarButtonItem = cancelButton;


loginRouter.loginViewController = loginViewController;

CardNavigationController *navigationController = [CardNavigationController darkBlueNavigationControllerWithClient:client];
navigationController.viewControllers = @[ loginViewController ];
navigationController.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;

[onViewController presentViewController:navigationController animated:YES completion:nil];

您可以在下面的ios 11中找到結果

在此處輸入圖片說明

檢查以下示例

UIImage *imgCart = [self imageWithImage:[UIImage imageNamed:@"ic_cart"] scaledToSize:CGSizeMake(35, 35)] ;

imgCart = [_utils changeColorOf:imgCart to:[UIColor whiteColor]];
UIButton *btnCart = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 30, 30)];
[btnCart addTarget:self action:@selector(btnCartClicked:) forControlEvents:UIControlEventTouchUpInside];
[btnCart setBackgroundImage:imgCart forState:UIControlStateNormal];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:btnCart];

當您在ios11中設置'cancelButton.tintColor = [UIColor whiteColor]'時,它不起作用。 您可以使用方法“ setTitleTextAttributes”。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM