gpt4 book ai didi

uinavigationcontroller - 自定义 UIBarButtonItem 与 iOS7 对齐

转载 作者:行者123 更新时间:2023-12-03 13:08:33 24 4
gpt4 key购买 nike

因此,在使用 UIButton 作为自定义 View 创建 UIBarButtonItem 时,我遇到了许多其他人遇到的相同问题。

基本上,该按钮距左侧或右侧大约 10 个像素。当我使用没有自定义 View 的常规 BarButtonItem 时,不会发生这种情况。

这篇文章提供了部分解决方案:
UIBarButton With Custom View

这是我通过子类化 UIButton 创建的代码(如另一篇文章中所述)

    - (UIEdgeInsets)alignmentRectInsets {
UIEdgeInsets insets;
if ([self isLeftButton]) {
insets = UIEdgeInsetsMake(0, 9.0f, 0, 0);
}
else { // IF ITS A RIGHT BUTTON
insets = UIEdgeInsetsMake(0, 0, 0, 9.0f);
}
return insets;
}


- (BOOL)isLeftButton {
return self.frame.origin.x < (self.superview.frame.size.width / 2);
}

这很好用,但是当我从导航 Controller 弹出一个 View Controller 回到这个主视图时,按钮仍然错误定位大约 0.3 秒,然后它卡入正确的插图。

这是一个巨大的眼睛,我不知道如何阻止它像这样折断。有什么想法吗?谢谢!

最佳答案

我和你和其他许多人一样有同样的问题。经过长时间的尝试修复它,我终于做到了。这是您必须包含在 *-Prefix.pch 文件中的类别。就这样!

UINavigationItem+iOS7Spacing.h

#import <Foundation/Foundation.h>
@interface UINavigationItem (iOS7Spacing)
@end

UINavigationItem+iOS7Spacing.m
#import "UINavigationItem+iOS7Spacing.h"
#import <objc/runtime.h>

@implementation UINavigationItem (iOS7Spacing)

- (BOOL)isIOS7
{
return ([[[UIDevice currentDevice] systemVersion] compare:@"7" options:NSNumericSearch] != NSOrderedAscending);
}

- (UIBarButtonItem *)spacer
{
UIBarButtonItem *space = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
space.width = -11;
return space;
}

- (void)mk_setLeftBarButtonItem:(UIBarButtonItem *)leftBarButtonItem
{
if ([self isIOS7] && leftBarButtonItem) {
[self mk_setLeftBarButtonItem:nil];
[self mk_setLeftBarButtonItems:@[[self spacer], leftBarButtonItem]];
} else {
[self mk_setLeftBarButtonItem:leftBarButtonItem];
}
}

- (void)mk_setLeftBarButtonItems:(NSArray *)leftBarButtonItems
{
if ([self isIOS7] && leftBarButtonItems && leftBarButtonItems.count > 0) {

NSMutableArray *items = [[NSMutableArray alloc] initWithCapacity:leftBarButtonItems.count + 1];
[items addObject:[self spacer]];
[items addObjectsFromArray:leftBarButtonItems];

[self mk_setLeftBarButtonItems:items];
} else {
[self mk_setLeftBarButtonItems:leftBarButtonItems];
}
}

- (void)mk_setRightBarButtonItem:(UIBarButtonItem *)rightBarButtonItem
{
if ([self isIOS7] && rightBarButtonItem) {
[self mk_setRightBarButtonItem:nil];
[self mk_setRightBarButtonItems:@[[self spacer], rightBarButtonItem]];
} else {
[self mk_setRightBarButtonItem:rightBarButtonItem];
}
}

- (void)mk_setRightBarButtonItems:(NSArray *)rightBarButtonItems
{
if ([self isIOS7] && rightBarButtonItems && rightBarButtonItems.count > 0) {

NSMutableArray *items = [[NSMutableArray alloc] initWithCapacity:rightBarButtonItems.count + 1];
[items addObject:[self spacer]];
[items addObjectsFromArray:rightBarButtonItems];

[self mk_setRightBarButtonItems:items];
} else {
[self mk_setRightBarButtonItems:rightBarButtonItems];
}
}

+ (void)mk_swizzle:(SEL)aSelector
{
SEL bSelector = NSSelectorFromString([NSString stringWithFormat:@"mk_%@", NSStringFromSelector(aSelector)]);

Method m1 = class_getInstanceMethod(self, aSelector);
Method m2 = class_getInstanceMethod(self, bSelector);

method_exchangeImplementations(m1, m2);
}

+ (void)load
{
[self mk_swizzle:@selector(setLeftBarButtonItem:)];
[self mk_swizzle:@selector(setLeftBarButtonItems:)];
[self mk_swizzle:@selector(setRightBarButtonItem:)];
[self mk_swizzle:@selector(setRightBarButtonItems:)];
}

@end

UINavigationItem+iOS7Spacing category on GitHub

关于uinavigationcontroller - 自定义 UIBarButtonItem 与 iOS7 对齐,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19233591/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com