【问题标题】:Objective-C - why the button is not aligned bottom center?Objective-C - 为什么按钮没有对齐底部中心?
【发布时间】:2017-08-03 02:32:10
【问题描述】:

我有未对齐底部中心的按钮?设置为全屏:

使用自动布局设置为底部中心:

#import "ViewController.h"

@interface ViewController ()
@end

@implementation ViewController

- (void)viewDidLoad {
  [super viewDidLoad];
  // Do any additional setup after loading the view, typically from a nib.
  UIButton *but= [UIButton buttonWithType:UIButtonTypeRoundedRect];
  [but addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
  int startHeight = 167;
  int frameHeight = self.view.frame.size.height - startHeight;
  [but setFrame:CGRectMake(0, startHeight, 320, frameHeight)];
  [but setTitle:@"Login" forState:UIControlStateNormal];
  [but setExclusiveTouch:YES];
  [but setImage:[UIImage imageNamed:@"Image"] forState:UIControlStateNormal];
  [self.view addSubview:but];


  NSDictionary *viewsDict = @{@"but" : but};
  [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-167-[but]-0-|"
                                                                    options:0 metrics:nil views:viewsDict]];
  [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-0-[but]-0-|"
                                                                    options:0 metrics:nil views:viewsDict]];
}


-(void) buttonClicked:(UIButton*)sender {
  NSLog(@"you clicked on button %@", sender.tag);
}

- (void)didReceiveMemoryWarning {
  [super didReceiveMemoryWarning];
  // Dispose of any resources that can be recreated.
}


@end

【问题讨论】:

    标签: ios objective-c iphone ipad autolayout


    【解决方案1】:

    使用自动布局设置为底部中心

    没有。您没有对任何事物的中心设置任何限制。

    【讨论】:

    • 您的约束很奇怪,以至于我希望控制台中出现错误消息!为什么不提?
    【解决方案2】:

    而不是使用

    [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-0-[but]-0-|"
    options:0 metrics:nil views:viewsDict]];
    

    使用

    self.view.addConstraint(NSLayoutConstraint(item: but, attribute: .centerX, relatedBy: .equal, toItem: self.view, attribute: .centerX, multiplier: 1.0, constant: 0))
    

    编辑

    实现OP的代码是Objective-C

    [self.view addConstraint:[NSLayoutConstraint constraintWithItem:but attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeCenterX multiplier:1.0 constant:0]];
    

    编辑

    这是我的 ViewDidLoad 中的代码

    - (void)viewDidLoad {
        [super viewDidLoad];
        UIButton *but = [[UIButton alloc] init];
        [but setTitle:@"ABCD" forState:UIControlStateNormal];
        [but setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
        int startHeight = 167;
        int frameHeight = self.view.frame.size.height - startHeight;
        [but setFrame:CGRectMake(0, startHeight, 320, frameHeight)];
        
        [but setTranslatesAutoresizingMaskIntoConstraints:false];
        [self.view addSubview:but];
        NSDictionary *viewsDict = @{@"button" : but};
        [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[button]-0-|"
                                                                          options:0 metrics:nil views:viewsDict]];
        NSLayoutConstraint *constraint = [NSLayoutConstraint constraintWithItem:but attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeCenterX multiplier:1.0 constant:0];
        constraint.active = true;
        [self.view addConstraint:constraint];
        [self.view layoutIfNeeded];
        // Do any additional setup after loading the view, typically from a nib.
    }
    

    这是输出:)

    像魅力一样工作:) 请检查你缺少什么声明??

    代码中的错误:

    1. 你错过了“[but setTranslatesAutoresizingMaskIntoConstraints:false];

    当您应用自动布局约束时,您会通知 iOS 不要将默认的自动调整大小掩码转换为自动布局约束,因为它可能与您应用的自动布局约束冲突:)

    2.constraint.active = true;

    创建约束后,请确保按照 EmilioPelaez 在 cmets 中完美指出的那样激活它:)

    【讨论】:

    • view.centerXAnchor.constraintEqualTo(but.centerXAnchor).isActive = true(或类似的东西,我是凭记忆写的)会是更好的方法。
    • @emiliopelaez:我同意 :)
    • @yumyumyum :你在控制台好友上遇到任何错误??
    • @yumyumyum : haha​​hahaha :) 我发布了一个 swift 代码好友 url 是 Objective C 因此编译错误:) 你需要 Objective-C 代码 lemme post it :)
    • @yumyumyum : 给我一分钟 :)
    猜你喜欢
    • 2019-11-26
    • 1970-01-01
    • 2017-01-29
    • 2017-01-04
    • 1970-01-01
    • 2017-02-16
    • 2018-06-25
    • 2021-06-10
    • 2019-07-26
    相关资源
    最近更新 更多