我有一个包含UIButton的UIView.UIButton为UIControlStateNormal("Follow")和UIControlStateSelected("Following")状态设置了2个标题.我在UIButton上使用自动布局,它有一个约束,距离超视图的顶部一定距离,另一个距离超视图的左侧有一定距离.我也使用了"尺寸适合内容".
当我从代码中将按钮设置为处于选定状态时,标题会正确更改,但UIButton的固有宽度不会更改,因此当从"Follow"更改为"Follow"时,文本将被椭圆化.
self.selected = self.following;
Run Code Online (Sandbox Code Playgroud)


当我以不同的方式处理问题时,只需在有人点击按钮时更改UIControlStateNormal的文本,该按钮就会正确地改变大小.
NSString *title = (self.following) ? @"Following" : @"Follow"
[self setTitle:title forState:UIControlStateNormal];
Run Code Online (Sandbox Code Playgroud)


这是UIKit中的一个错误吗?我希望按钮可以更改其内在大小,以便在状态发生变化时正确反映文本的新大小,特别是因为除了2按钮状态的文本之外还有其他我想要更改的内容.
我在Android应用中遇到按钮文字对齐问题.我有一个XML定义的按钮,如下所示:
<Button
android:id="@+id/reportanissuebutton"
android:layout_width="272sp"
android:layout_height="32sp"
android:text="@string/button_report_issue"
android:textColor="#fff"
android:textSize="18sp"
android:gravity="center_vertical|center_horizontal"
android:background="@xml/report_issue_button"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="40sp"/>
Run Code Online (Sandbox Code Playgroud)
结果是这样的:
我希望文本在垂直和水平方向都居中,就像我的应用程序中的所有其他按钮一样,但由于某种原因,它会略微向下偏移.我似乎无法弄清楚为什么这样做,所以任何帮助将不胜感激.
编辑:这是按钮背景的XML:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<corners android:radius="6dp" />
<stroke android:color="#c2c2c2" android:width="2dp" />
<gradient
android:angle="90"
android:startColor="#000"
android:endColor="#919191"/>
</shape>
Run Code Online (Sandbox Code Playgroud)