Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/116.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
iOS添加UIButton源代码:can';t控件UIButton标题栏文本对齐_Ios_Objective C_Uibutton - Fatal编程技术网

iOS添加UIButton源代码:can';t控件UIButton标题栏文本对齐

iOS添加UIButton源代码:can';t控件UIButton标题栏文本对齐,ios,objective-c,uibutton,Ios,Objective C,Uibutton,我在ui视图中添加了ui按钮 UIButton *switchButtonL = [UIButton buttonWithType: UIButtonTypeSystem]; [switchButtonL addTarget:self action:@selector(switchCity:) forControlEvents:UIControlEventTouchUpInside]; switchButtonL.frame = CGRectMake(0, 50, se

我在
ui视图中添加了
ui按钮

    UIButton *switchButtonL = [UIButton buttonWithType: UIButtonTypeSystem];
    [switchButtonL addTarget:self action:@selector(switchCity:) forControlEvents:UIControlEventTouchUpInside];
    switchButtonL.frame =  CGRectMake(0, 50, self.screenWidth/2, 30);
    [switchButtonL setTitle: NSLocalizedString(@"switchcity", nil) forState:UIControlStateNormal];
    [switchButtonL setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [switchButtonL setTitleColor:[UIColor blueColor] forState:UIControlStateSelected];
    [switchButtonL setBackgroundColor:[UIColor clearColor] ];
    switchButtonL.titleLabel.textAlignment = NSTextAlignmentLeft; // this line can't work
    switchButtonL.titleLabel.font = [UIFont fontWithName:@"HelveticaNeue-Light" size:18];
    [self addSubview:switchButtonL];
注释行无法工作,文本始终居中对齐。

您可以从上图所示的xib更改它。如果您想通过代码进行更改,请在您所需的职位上使用:

self.yourbtn.titleEdgeInsets=UIEdgeInsetsMake(topVal, leftVal, downVal, rightVal); 

您可以从上图所示的xib更改它。如果您想通过代码进行更改,请在您所需的职位上使用:

self.yourbtn.titleEdgeInsets=UIEdgeInsetsMake(topVal, leftVal, downVal, rightVal); 
试试这个

switchButtonL.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
switchButtonL.contentEdgeInsets = UIEdgeInsetsMake(0, 10, 0, 0);
试试这个

switchButtonL.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
switchButtonL.contentEdgeInsets = UIEdgeInsetsMake(0, 10, 0, 0);
试用替代品

switchButtonL.titleLabel.textAlignment = NSTextAlignmentLeft; // this line can't work
与:

试用替代品

switchButtonL.titleLabel.textAlignment = NSTextAlignmentLeft; // this line can't work
与:


如果任何答案与u相关,请打勾如果任何答案与u相关,请打勾