【发布时间】:2016-10-26 07:22:33
【问题描述】:
我使用以下代码制作了一个浮动图标。
- (void)createFloatingButton
{
UIButton *floatingButton = [UIButton buttonWithType:UIButtonTypeCustom];
floatingButton.frame = CGRectMake(kScreenWidth-kFloatingButtonSize-kFloatingButtonSize/2, self.view.frame.size.height-kFloatingButtonSize-kFloatingButtonSize/2, kFloatingButtonSize,kFloatingButtonSize);
[floatingButton setBackgroundColor:kNavColor];
floatingButton.layer.cornerRadius = kFloatingButtonSize/2;
//Configre Font and Text
[floatingButton setTitle:@"+" forState:UIControlStateNormal];
floatingButton.titleLabel.font = [UIFont systemFontOfSize:30];
[floatingButton addTarget:self action:@selector(floatingButtonTapped) forControlEvents:UIControlEventTouchUpInside];
//Mange Alignment
floatingButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
floatingButton.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
//Add shadow
floatingButton.layer.shadowColor = [[UIColor blackColor] CGColor];
floatingButton.layer.shadowOffset = CGSizeMake(0, 2.0f);
floatingButton.layer.shadowOpacity = 0.6f;
floatingButton.layer.shadowRadius = 3.0f;
floatingButton.layer.masksToBounds = NO;
[self.view addSubview:floatingButton];
}
我的输出是这样的:
我无法将+ 置于中心位置。
【问题讨论】:
-
您可以使用带 + 的图像来代替所有这些操作。用 + 本身制作一张图像。对于 + 来说,这很容易没有问题
-
@Jecky In Image 我必须添加点击手势,或者如果我将 UIImage 放在 UIButton 后面,代码会增加,我不想使用 Imgaege。
-
"floatingButton.titleLabel.textAlignment = NSTextAlignmentCenter;"应该很有用。
-
@alicanbatur 不工作
-
如果你必须使用 + 作为字符串,你应该改变字体大小以获得最佳外观。如果您不必通过字符串执行此操作,则使用图像将解决您的解决方案。
标签: ios objective-c ios8 uibutton