这个问题让我困惑了几天。无法隐藏编程式创建的UIButton
我有2个UIButtons,一个是使用Storyboard创建的,另一个是以编程方式创建的。
当我按下其中一个按钮时,我被移动到隐藏编程构建和故事板按钮的方法,但只有故事板按钮消失,而编程构建的按钮仍然存在。我试过的不仅是隐藏,还改变了编程式构建按钮的框架,但无济于事。 下面是使用的代码:
@property (strong, nonatomic) UIButton *catBut;
@property (weak, nonatomic) IBOutlet UIButton *whenButton;
....
....
-(void)viewDidLoad {
....
[self customizeButton:self.catBut withFrame:CGRectMake(165, 113, 135, 50)
andAction:@selector(selectedCat) andColor:[UIColor belizeHoleColor]
andTag:2 andImage:@"coffee-64.png" andText:@"Cafes" andAlignmentLeft:YES];
....
}
- (void)customizeButton:(UIButton *)but withFrame:(CGRect)rect andAction:(SEL)action
andColor:(UIColor *)col andTag:(NSUInteger)tag
andImage:(NSString *)imageName
andText:(NSString *)buttonText
andAlignmentLeft:(BOOL)alignLeft {
but = [UIButton buttonWithType:UIButtonTypeCustom];
[but setFrame:rect];
[but setTitle:buttonText forState:UIControlStateNormal];
but.tag = tag;
but.titleLabel.numberOfLines = 0;
but.titleLabel.font = [UIFont boldFlatFontOfSize:18];
[but setImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal];
CGFloat spacing = 10; // the amount of spacing to appear between image and title
but.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
but.imageEdgeInsets = UIEdgeInsetsMake(0, spacing, 0, 0);
but.titleEdgeInsets = UIEdgeInsetsMake(0, spacing*2, 0, 0);
[but setTitleColor:[UIColor cloudsColor] forState:UIControlStateNormal];
[but setTitleColor:[UIColor cloudsColor] forState:UIControlStateHighlighted];
[but addTarget:self action:action forControlEvents:UIControlEventTouchUpInside];
[self.buttonsView addSubview:but];
}
....
- (void)selectedCat {
self.catBut.hidden = YES;
self.whenButton.hidden = YES;
}
为什么只有故事板按钮符合用户界面的变化?有什么我失踪?我已经尝试取消AutoLayout,但这并没有改变一件事。
您解决了问题!非常感谢。 – Yarneo
我有一种预感,它与UIButton指针的传递有关。 – Yarneo
好,你的问题解决:) – nanjunda