目前,我以编程方式在UIView
内创建了7个按钮。我在UIlabel
上添加了两行UIButton
。我创建了另一个UIView
作为选择器,每当单击按钮时,我都会将选择器移到按钮的位置。
问题是选择器UIView
是按钮顶部的另一层,颜色混合或失真。
要以编程方式在按钮上插入两行标题,并在选择按钮时更改其背景颜色。
-(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;
}
答案 0 :(得分:0)
我认为您不需要使用辅助按钮或其他标签。 您可以使用按钮本身来完成所有工作。 以下内容可以将其转换为Objective-C:
答案 1 :(得分:0)
您可以使用自定义按钮,该按钮从sudo /bin/pip2 install MySQL-python
继承两个标签,并重写其UIButton
方法,例如:
setSelected: