【问题标题】:iOS - Resize UISwitch in UINavigationBariOS - 在 UINavigationBar 中调整 UISwitch 的大小
【发布时间】:2015-03-14 04:09:25
【问题描述】:

我正在尝试在我的UINavigationBar 中使用UISwitch。我想要一个比默认值更小的开关,所以我用anonymousSwitch.transform = CGAffineTransformMakeScale(0.55, 0.55); 缩小尺寸。当我这样做时,开关会缩小,但我不知道如何将它与UINavigationBar 中的其他元素垂直对齐。 这是目前的样子:

理想情况下,UISwitch 与 175 UILabel 的间距与 X UIButton 与 175 UILabel 的间距相同,UISwitch 与其余部分在一条直线上UINavigationBar 中的元素。我尝试做switch.center = CGPointMake(switch.center.x, shareButton.center.y),但这根本不影响UISwitch 的放置。 有什么方法可以让我按自己的意愿定位开关吗?

【问题讨论】:

  • 当使用transform 时,我建议不要使用自动布局。约束不适用于transform。删除所有约束,然后更改center

标签: ios objective-c ios8 uinavigationbar uiswitch


【解决方案1】:

我遇到了同样的问题。
我试图解决它并想出了这个解决方案。
我认为这不是一个完美的解决方案,并且可能不适用于每个 iOS 版本。

首先你需要像这样子类化 UISwitch:

@interface UICustomSwitch : UISwitch

@property (assign, nonatomic) CGFloat scale;

@end

@implementation UICustomSwitch

- (void)setFrame:(CGRect)frame {
    CGFloat superview_height = self.superview.frame.size.height;
    frame.origin.y = (superview_height - frame.size.height * _scale) / 2;
    [super setFrame:frame];
}

@end

要做的第二件事是像这样创建和设置您的 UICustomSwitch:

UICustomSwitch* switch = [UICustomSwitch new];
switch.transform = CGAffineTransformMakeScale(0.75, 0.75);
switch.scale = 0.75;
UIBarButtonItem* button = [[UIBarButtonItem alloc] initWithCustomView:switch];

要在 175 和交换机之间创建更大的空间,您可以在 175 和交换机之间添加另一个 UIBarButtonItem。例如。具有特定宽度的固定间隔。

就像我说的,我不认为这是一个完美的解决方案。
在我的测试中,它起作用了。

0yeoj 的另一个解决方案(但稍作调整 -> 始终为每个 navigationBar 高度居中,对于工具栏它是相同的,只需使用 self.navigationController.toolbar.frame.size.height ):

UIView* switch_container = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 44, self.navigationController.navigationBar.frame.size.height)];

UISwitch* switch = [[UISwitch alloc] initWithFrame:switch_container.bounds];
switch.transform = CGAffineTransformMakeScale(0.75, 0.75);
switch.center = CGPointMake(switch_container.bounds.size.width/2, switch_container.bounds.size.height/2);
[switch addTarget:self action:@selector(action:) forControlEvents:UIControlEventValueChanged];
[switch_container addSubview:switch];

UIBarButtonItem* button = [[UIBarButtonItem alloc] initWithCustomView:switch_container];

self.navigationItem.rightBarButtonItem = button;

【讨论】:

    【解决方案2】:

    您是否尝试过使用另一种转换方法来移动开关,例如,

    CGAffineTransformMakeTranslation(xValue, yValue);
    

    您也可以将两个变换合并在一起,并让该变换像切换一样,

    CGAffineTransform scale= CGAffineTransformMakeScale(0.55, 0.55);
    
    CGAffineTransform translate = CGAffineTransformMakeTranslation(0, 10);
    
    anonymousSwitch.transform = CGAffineTransformConcat(scale, translate);
    

    【讨论】:

    • 转换没有做任何事情。
    【解决方案3】:

    这是我测试的代码..

    - (void)viewDidLoad {
        [super viewDidLoad];
        // Do any additional setup after loading the view, typically from a nib.
    
        UIButton *xbutton = [UIButton buttonWithType:UIButtonTypeCustom];
        xbutton.frame = CGRectMake(0, 6, 35, 35);
        xbutton.backgroundColor = [UIColor redColor];
    
        UIView *containerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 44, 44)];
    
        UISwitch *switchButton = [[UISwitch alloc] init];
        switchButton.frame = CGRectMake(0, 0, 35, 35);
        switchButton.transform = CGAffineTransformMakeScale(0.55, 0.55);
        switchButton.center = CGPointMake(containerView.frame.size.width/2, containerView.frame.size.height/2);
        [containerView addSubview:switchButton];
    
        UIBarButtonItem *additionalButton = [[UIBarButtonItem alloc] initWithCustomView:containerView];
    
        UIBarButtonItem *xBarButton = [[UIBarButtonItem alloc] initWithCustomView:xbutton];
    
        //rearrange it if necessary
        NSArray *arrayButton = [NSArray arrayWithObjects: xBarButton, additionalButton, nil];
    
        self.navigationItem.leftBarButtonItems = arrayButton;
    }.
    

    嗯...我还没有降级到优胜美地,所以我这里没有 ios8.3 这个代码对我来说非常适合..

    【讨论】:

    • 您的解决方案对我不起作用。你测试了吗?在 iPhone 6、iOS 8.3 上进行了测试。不过,我的做法确实有效。
    • 实际上在 iOS 8.2 上.. 是的,我刚刚尝试过.. 我将编辑并放置整个代码..
    • 尴尬。我刚才在 iPhone 5 (7.1) Simulator 上运行了它。在工具栏/导航栏中放置一个带有我的解决方案的按钮和一个带有您的解决方案的按钮,并且您的按钮未垂直居中。别问我为什么,我就是用了你的代码。
    • 是的,我认为它不会垂直对齐,因为您手动设置了switch.center = CGPointMake(switch.center.x, shareButton.center.y)。我认为其他按钮也一样?看看我是如何设置框架的..尝试复制粘贴它,看看会发生什么..
    • 使用你的 containerView 让它工作。但是当工具栏高度发生变化时,偏移量就会关闭。
    猜你喜欢
    • 2011-02-26
    • 2023-03-16
    • 2011-04-02
    • 1970-01-01
    • 1970-01-01
    • 2013-02-18
    • 1970-01-01
    • 2013-10-03
    • 1970-01-01
    相关资源
    最近更新 更多