objective-c - UIbutton無法點選
高洛峰
高洛峰 2017-05-02 09:33:23
0
0
512

傳到Github的文件(很小)

在viewcontroller裡面增加了一個UIView類LeftMenuView,
LeftMenuView裡面增加了一個頂部view和底部view,
各添加了一個button,這兩個button無法點擊。
userInteractionEnabled是YES,
button的frame在 View Debugging裡面顯示沒有超過父視圖。
部分程式碼:
ViewController:

- (void)viewDidLoad {
    [super viewDidLoad];
    LeftMenuView *leftView=[LeftMenuView createLeftMenuView];
    leftView.userInteractionEnabled=YES;
    self.leftView=leftView;
    [self.view addSubview:leftView];
    //nightButton
    UIButton *nightButton=[self.leftView viewWithTag:6];
    [nightButton addTarget:self action:@selector(touchButton:) forControlEvents:UIControlEventTouchUpInside];
    leftView.userInteractionEnabled=YES;
//    [self.leftView addSubview:nightButton];
    //set
    UIButton *settingButton=[self.leftView viewWithTag:4];
    [settingButton addTarget:self.leftView action:@selector(push) forControlEvents:UIControlEventTouchUpInside];
    }
    

LeftMenuView:這個是UIview,好像不該在view裡面加入事件吧? ? ?

-(instancetype)initWithFrame:(CGRect)frame
{
    self=[super initWithFrame:frame];
    self.backgroundColor=[UIColor colorWithRed:26/256.f green:31/256.f blue:36/256.f alpha:0.7];
    [self addHeaderView];
    [self addButton];
    return self;
}
#pragma mark -add view to menu view
-(void)addHeaderView
{
    UIView *headViewUp=[[UIView alloc]init];
    headViewUp.userInteractionEnabled=YES;
    headViewUp.frame=CGRectMake(0, 0, yScreenWidth/2, 114);
    headViewUp.backgroundColor=ColorWithRGB(26, 31, 36);
    headViewUp.backgroundColor=[UIColor redColor];
    self.headViewUp=headViewUp;
    [self addSubview:headViewUp];
    UIView *headViewDown=[[UIView alloc]init];
    headViewDown.userInteractionEnabled=YES;
    headViewDown.frame=CGRectMake(0, yScreenHeight-50, yScreenWidth/2, 50);
    headViewDown.backgroundColor=ColorWithRGB(26, 31, 36);
    self.headViewDown=headViewDown;
    [self addSubview:headViewDown];
}
-(void)addButton
{
    UIButton *settingButton=[self setFrame:CGRectMake(yScreenWidth*2/6, 64, yScreenWidth/6, 50 ) title:@"设置" image:@"Menu_Icon_Setting"];
    settingButton.userInteractionEnabled=YES;
    settingButton.tag=4;
    [self resizeTextAndImageInButton:settingButton];
    [self.headViewUp addSubview:settingButton];
    UIButton *nightButton=[self setFrame:CGRectMake(yScreenWidth/4, 0, yScreenWidth/4, 50 ) title:@"白天" image:@"Menu_Day"];
    nightButton.tag=6;
    [self.headViewDown addSubview:nightButton];
}
-(UIButton *)setFrame:(CGRect)frame title:(NSString *)title image:(NSString *)imageName
{
    UIButton *button=[[UIButton alloc]initWithFrame:frame];
    [button setTitle:title forState:UIControlStateNormal];
    [button setImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal];
    button.titleLabel.font=[UIFont systemFontOfSize:8];
    button.contentHorizontalAlignment=UIControlContentHorizontalAlignmentCenter;
    return button;
}
-(void)resizeTextAndImageInButton:(UIButton *)button
{
    [button setTitleEdgeInsets:UIEdgeInsetsMake(button.imageView.frame.size.height ,-button.imageView.frame.size.width, 0.0,0.0)];
    [button setImageEdgeInsets:UIEdgeInsetsMake(-10, 0.0,0.0, -button.titleLabel.bounds.size.width)];
}
高洛峰
高洛峰

拥有18年软件开发和IT教学经验。曾任多家上市公司技术总监、架构师、项目经理、高级软件工程师等职务。 网络人气名人讲师,...

全部回覆(0)
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!