【发布时间】:2015-08-06 04:22:48
【问题描述】:
我需要在标题标签旁边(右侧)放置一个图像视图, 所以我用以下方法创建了一个类别:
- (void)updateInsetsToMoveImageRightToText:(NSString *)text {
CGFloat textWidth = [text sizeWithAttributes:[self.titleLabel.attributedText attributesAtIndex:0 effectiveRange:NULL]].width;
CGFloat newXPosition = (self.frame.size.width - (self.frame.size.width - textWidth) / 2);
self.imageEdgeInsets = UIEdgeInsetsMake(0., newXPosition, 0., 0.);
self.titleEdgeInsets = UIEdgeInsetsMake(0., 0., 0., self.imageView.image.size.width);
}
我是这样使用的:
- (void)quizzesFilterView:(QuizzesFilterView *)filterView didSelectFilter:(QuizFilterType)type {
self.filterType = type;
NSString *newTitle = [QuizzesFilterView filterNameByType:type];
[self.filterButton setTitle:newTitle forState:UIControlStateNormal];
[self.filterButton updateInsetsToMoveImageRightToText:newTitle];
[self removeFilterView];
[self updateSearchedQuizzesWithSearchText:nil quizFilterType:type];
[self updateTableUi];
}
但它不适用于较小的标题:
我做错了什么?
【问题讨论】:
-
你在哪里调用该代码?基于
frame的计算是错误的,例如在viewDidLoad中(即当实际帧尚未计算时) -
@Azat 每次过滤器值发生变化时我都会调用它,它是从放置在按钮下方的表格视图中选择的。
标签: ios objective-c iphone cocoa-touch uibutton