ios - 将 subview 保留为 UIStackView 中的原始尺寸

标签 ios objective-c uibutton uistackview

目前我正在尝试在水平组中制作 3 个具有相等间距的 3 个按钮

UIStackView * menuButtons = [[UIStackView alloc] initWithFrame:CGRectMake(0, 0, 60, 16)];
  menuButtons.axis = UILayoutConstraintAxisHorizontal;
  menuButtons.alignment = UIStackViewAlignmentBottom;
  menuButtons.spacing = 6;
  menuButtons.distribution = UIStackViewDistributionEqualSpacing;

UIButton* btnOne = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 16, 16)];
UIButton* btnTwo = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 16, 16)];
UIButton* btnThree = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 16, 16)];

  [menuButtons addArrangedSubview:btnOne];
  [menuButtons addArrangedSubview:btnTwo];
  [menuButtons addArrangedSubview:btnThree];

我注意到,当我以编程方式将按钮 View 添加到我的堆栈 View 后检查它们的框架时,它们都显示出与最初设置不同的大小,而我希望它们仍然是 16

即:btnOne CGSize(17, 16), btnTwo CGSize(23.5, 23,5), btnThree CGSize(21.3, 21.3)

我不明白为什么会这样,我已经尝试了所有的发行版,但是当我没有在其他任何地方设置这些 View 的框架时,我无法弄清楚这一点

最佳答案

这是一个示例 - 基于您的代码 - 使用自动布局约束。

请注意,您不需要为 UIStackView 指定宽度和高度限制,因为它会根据需要扩展以适应排列的 subview 。

如果您确实设置了宽度限制,您最终会得到(可能)意想不到的结果,因为您指定了6 的固定宽度间距。

所以,您可能想要:

  1. 宽度约束 + UIStackViewDistributionEqualSpacing,或者
  2. 无宽度限制 + 默认分布 + .spacing = 6

希望这有助于...

//
//  StackTestViewController.m
//
//  Created by Don Mag on 6/24/17.
//

#import "StackTestViewController.h"

@interface StackTestViewController ()

@end

@implementation StackTestViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupMenuButtons];
}

- (void)setupMenuButtons {

    // instantiate a UIStackView
    UIStackView *menuButtons = [[UIStackView alloc] init];

    // horizontal
    menuButtons.axis = UILayoutConstraintAxisHorizontal;

    // spacing between arranged views
    menuButtons.spacing = 6;

    // instantiate 3 buttons
    UIButton* btnOne = [UIButton new];
    UIButton* btnTwo = [UIButton new];
    UIButton* btnThree = [UIButton new];

    // give 'em background colors so we can see them
    btnOne.backgroundColor = [UIColor redColor];
    btnTwo.backgroundColor = [UIColor greenColor];
    btnThree.backgroundColor = [UIColor blueColor];

    // for each button,
    //  tell it not to use Autoresizing Mask
    //  set width and height constraints each to 16
    for (UIButton *b in @[btnOne, btnTwo, btnThree]) {
        [b setTranslatesAutoresizingMaskIntoConstraints:NO];
        [b.widthAnchor constraintEqualToConstant:16.0].active = YES;
        [b.heightAnchor constraintEqualToConstant:16.0].active = YES;
    }

    // add them to the Stack View
    [menuButtons addArrangedSubview:btnOne];
    [menuButtons addArrangedSubview:btnTwo];
    [menuButtons addArrangedSubview:btnThree];

    // add the Stack View to self view
    [self.view addSubview:menuButtons];

    // tell Stack View not to use Autoresizing Mask
    [menuButtons setTranslatesAutoresizingMaskIntoConstraints:NO];

    // set X and Y position for the Stack View (just using 80,80 for this example)
    [menuButtons.leadingAnchor constraintEqualToAnchor:self.view.leadingAnchor constant:80.0].active = YES;
    [menuButtons.topAnchor constraintEqualToAnchor:self.view.topAnchor constant:80.0].active = YES;

}

#end

关于ios - 将 subview 保留为 UIStackView 中的原始尺寸,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44739520/

相关文章:

ios - 构建iOS ipa时如何选择推送通知类型

ios - SKLableNode - 使用图像作为颜色模式

iphone - 编译错误与 : switch, "expected expression before"

ios - 带有 < 符号的自定义后退按钮

ios - 带有突出显示底部边框的 UIButton

ios - 自定义委托(delegate)不更新 iOS 中的 UI

ios - UILocalNotification 中的声音在最大音量时比 AVAudioPlayer 响亮

ios - 删除不存在的 key 时,Firebase 数据库不会返回错误

ios - 如何在请求中使用 NSStrings 映射 NSArray (RESTKit)

iphone - 如何沿 y 轴 iPhone 向下移动按钮操作