【问题标题】:Objective C-Programmatically insert the title of UIButton, change the background and title color when selectedObjective C-以编程方式插入UIButton的标题,选中时更改背景和标题颜色
【发布时间】:2018-09-05 06:58:15
【问题描述】:

目前,我以编程方式在 UIView 中创建了 7 个按钮。我在UIButton 中添加了两行UIlabel。我创建了另一个UIView 作为选择器,每当单击按钮时,我都会将选择器移动到按钮的位置。 问题是选择器UIView 是按钮顶部的另一层,颜色混合或失真。

我所拥有的以及部分尝试实现的目标

我想要实现的目标

以编程方式在按钮中插入 2 行标题,并在按钮被选中时更改按钮的背景颜色。

代码

-(void)setupSegmentButtons{
    CGFloat Y_POS_BTN = [[UIApplication sharedApplication] statusBarFrame].size.height+5;

    //=== The view where the buttons sits
    navigationView = [[UIView alloc]initWithFrame:CGRectMake(X_BUFFER,Y_POS_BTN,self.view.frame.size.width,HEIGHT_BTN)];
    navigationView.backgroundColor = [UIColor whiteColor];

    [self.view addSubview:navigationView]; //=== Create a View called navigationView

    //==== Setup the shadows around the view ===
    UIBezierPath *shadowPath = [UIBezierPath bezierPathWithRect:self.navigationView.bounds];
    self.navigationView.layer.masksToBounds = NO;
    self.navigationView.layer.shadowColor = [UIColor lightGrayColor].CGColor;
    self.navigationView.layer.shadowOffset = CGSizeMake(5.0f, 5.0f);
    self.navigationView.layer.shadowOpacity = 0.8f;
    self.navigationView.layer.shadowPath = shadowPath.CGPath;

    //=== Get the dates and formatting of the dates
    NSDate *now = [NSDate date];
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSDate *beginningOfThisWeek;
    NSTimeInterval durationOfWeek;

    [calendar rangeOfUnit:NSWeekCalendarUnit
            startDate:&beginningOfThisWeek
             interval:&durationOfWeek
              forDate:now];

    NSDateComponents *comps = [calendar components:NSUIntegerMax fromDate:now];

    NSDateFormatter *dateFormatter=[[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"dd/MM/YYYY"];
    NSDateFormatter *datelblFormat = [[NSDateFormatter alloc] init];
    [datelblFormat setDateFormat:@"dd"];
    NSDateFormatter *daylblFormat= [[NSDateFormatter alloc] init];
    [daylblFormat setDateFormat:@"EEE"];

    //=== Loop 7 times to create the Buttons and the 2 lines Labels
    for (int i = 0; i<numControllers; i++) {
    
        button = [[UIButton alloc]initWithFrame:CGRectMake(i*  (self.navigationView.frame.size.width/numControllers), Y_BUFFER,  (self.navigationView.frame.size.width/numControllers),HEIGHT_BTN)];

        [navigationView addSubview:button]; //=== Put the buttons into the navigation View
    
        NSString *dateString = [dateFormatter stringFromDate:[calendar dateFromComponents:comps]];
        [dtDate addObject:dateString];
    
        NSString *lblDate = [datelblFormat stringFromDate:[calendar dateFromComponents:comps]];
    
        firstLineButton = [[UILabel alloc] initWithFrame:CGRectMake(0,5,self.view.frame.size.width/numControllers,HEIGHT_LABEL)];
    
        firstLineButton.text = lblDate;
        firstLineButton.font = [UIFont systemFontOfSize:20];
        firstLineButton.textColor = [UIColor whiteColor];
        firstLineButton.textAlignment=NSTextAlignmentCenter;
        [button addSubview:firstLineButton]; //=== Put the Date in the 1st line of the the button
    
        NSString *lblDay = [daylblFormat stringFromDate:[calendar dateFromComponents:comps]];
    
        UILabel *secondLineButton = [[UILabel alloc] initWithFrame:CGRectMake(0,28,self.view.frame.size.width/numControllers,HEIGHT_LABEL2)];
        secondLineButton.text = lblDay;
        secondLineButton.textColor = [UIColor whiteColor];
        secondLineButton.font = [UIFont boldSystemFontOfSize:11];
        secondLineButton.textAlignment=NSTextAlignmentCenter;
        [button addSubview:secondLineButton]; //=== Put the Day in the 2nd line of the Button
    
        if(i < 6){
            UIView *separator = [[UIView alloc] initWithFrame:CGRectMake(button.frame.size.width - 1, 10, 1, button.frame.size.height - 25)];
            separator.backgroundColor = UIColor.whiteColor;
            [button addSubview:separator];
        }
    
        button.tag = i; //--- IMPORTANT: if you make your own custom buttons, you have to tag them appropriately
    
        button.backgroundColor = [UIColor colorWithRed:236.0f/255.0f green:0/255.0f blue:140.0f/255.0f alpha:0.6];//%%% buttoncolors
    
        [button addTarget:self action:@selector(tapSegmentButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    
        ++comps.day;
    }

    //NSLog(@" The array %@", dtDate);

    [self setupSelector]; //=== The selection bar or highligthed area
}

//=== sets up the selection bar under the buttons or the highligted buttons on the navigation bar
-(void)setupSelector {

    selectionBar = [[UIView alloc]initWithFrame:CGRectMake(0, 0, (self.view.frame.size.width/numControllers),HEIGHT_BTN)];
    selectionBar.backgroundColor = [UIColor colorWithRed:1.0f/255.0f green:179.0/255.0f blue:242.0f/255.0f alpha:0.6]; //%%% sbcolor
    [navigationView addSubview:selectionBar];
}

//=== When the top button is tapped
 -(void)tapSegmentButtonAction:(UIButton *)button {

     sDtDate = dtDate[button.tag];

     [self LoadClasses];
 
     __weak typeof(self) weakSelf = self;
     [weakSelf updateCurrentPageIndex:button.tag];

     NSInteger xCoor = selectionBar.frame.size.width*self.currentPageIndex;

     selectionBar.frame = CGRectMake(xCoor, selectionBar.frame.origin.y, selectionBar.frame.size.width, selectionBar.frame.size.height);
}

//=== makes sure the nav bar is always aware of what date page you're at in   reference to the array of view controllers you gave
-(void)updateCurrentPageIndex:(int)newIndex {

    self.currentPageIndex = newIndex;
}

【问题讨论】:

  • 我还没有遇到您的问题。我的建议是继承 UIButton 以获得更清洁的解决方案。然后按住当前活动按钮作为 viewController 中的参考。选择另一个按钮时,您必须将ActiveButton的背景颜色设置为默认颜色所选按钮的颜色为活动颜色。 span>

标签: objective-c uiview uibutton


【解决方案1】:

我认为您不需要使用辅助按钮或附加标签。 你可以用按钮本身做所有的事情。 以下可以通过将其转换为 Objective-C 来帮助您:

Swift - UIButton with two lines of text

【讨论】:

    【解决方案2】:

    您可以使用继承自UIButton 的自定义按钮,带有两个标签,并将其重写为setSelected: 方法,例如:

    #import "MyButton.h"
    
    @interface MyButton ()
    
    @property (nonatomic, strong) UILabel *mainTitleLabel;
    @property (nonatomic, strong) UILabel *subTitleLabel;
    
    @end
    
    @implementation MyButton
    
    - (instancetype)initWithFrame:(CGRect)frame {
        if (self = [super initWithFrame:frame]) {
            self.mainTitleLabel = [[UILabel alloc] init];
            self.subTitleLabel = [[UILabel alloc] init];
            // other setting
        }
        return self;
    }
    
    - (void)setSelected:(BOOL)selected {
        [super setSelected:selected];
    
        if (selected) {
            self.backgroundColor = [UIColor whiteColor];
            self.mainTitleLabel.textColor = [UIColor redColor];
            self.subTitleLabel.textColor = [UIColor redColor];
        } else {
            self.backgroundColor = [UIColor blueColor];
            self.mainTitleLabel.textColor = [UIColor orangeColor];
            self.subTitleLabel.textColor = [UIColor orangeColor];
        }
    }
    
    @end
    

    【讨论】:

      猜你喜欢
      • 2012-09-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-12-14
      • 1970-01-01
      相关资源
      最近更新 更多