【问题标题】:Moved view items in left menu SWRevealViewController objective c在左侧菜单 SWRevealViewController 目标 c 中移动了视图项
【发布时间】:2017-04-04 19:22:56
【问题描述】:

我在目标 c 的项目中使用 SWRevealViewController。有时当我向左滑动菜单时,会移动第一项。只有第一项,我不知道为什么,因为它与其他项具有相同的边距。

另外,它不是表格视图,它只是在故事板中创建的静态视图。

有时显示不正确

应该是这样的

第一项有时会移动。

有什么想法吗?谢谢帮助;)

【问题讨论】:

  • 无法得到您的问题,请更具体地解释..
  • @vaibhav 问题是修复 uilabel 填充位置。有时第一行文本设置不正确
  • @Vinodh 没错……
  • @lost.in.code SWRevealViewController's 左侧菜单配置了tableview,并且您错误的标签位置放置在单元格内,因此只需检查位置和文本对齐方式.. 可能会解决。跨度>

标签: ios objective-c viewcontroller swrevealviewcontroller


【解决方案1】:

//修改后的代码

//你可以用你自己的来创造一切。这是使用 swreveal 的非常简单的方法。我希望它会有所帮助。

NSArray *menuItems;

menuItems = @[@"title",@"Edit Profile",@"Match",@"Add Event",@"Give",@"Events",@"Market place",@"Recognizes businesses",@"About",@"Termsnuse",@"changePassword",@"Logout"];


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

{
return [menuItems count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{


NSString *CellIdentifier = [menuItems objectAtIndex:indexPath.row];
UIImageView*imgProfile=[[UIImageView alloc]initWithFrame:CGRectMake(10, 20, 50, 50)];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

UIView *bgColorView = [[UIView alloc] init];
bgColorView.backgroundColor = Black;
[cell setSelectedBackgroundView:bgColorView];

if (indexPath.row==0) {
    UILabel*lbl;
    lbl=[[UILabel alloc]initWithFrame:CGRectMake(0, 0, 320, 130)];

    UILabel*lblUserName;
    UIImageView*imgUser;
    imgUser=[[UIImageView alloc]initWithFrame:CGRectMake(100, 10, 80, 80)];

    lblUserName=[[UILabel alloc]initWithFrame:CGRectMake(40, 95, 200, 30)];

    lbl.backgroundColor=Black;
    imgProfile.backgroundColor=[UIColor orangeColor];

    NSUserDefaults*defaults=[NSUserDefaults standardUserDefaults];


     NSDictionary *dict = [defaults objectForKey:@"Ucountry"];



    NSString *strImage = [dict objectForKey:@"image"];

    if ([strImage isEqualToString:@""]) {

        NSLog(@"strImage====>%@",strImage);
//            strImage = [strImage stringByReplacingOccurrencesOfString:@" " withString:@""];
//            NSURL *imageURL = [[NSURL alloc]initWithString:strImage];
//            [imgUser sd_setImageWithURL:imageURL placeholderImage:[UIImage imageNamed:@"user"]];
    }else{
        strImage = [strImage stringByReplacingOccurrencesOfString:@" " withString:@""];
        NSURL *imageURL = [[NSURL alloc]initWithString:strImage];
        [imgUser sd_setImageWithURL:imageURL placeholderImage:[UIImage imageNamed:@"user"]];
    }







    imgUser.layer.cornerRadius=40;
    imgUser.clipsToBounds=YES;
    imgUser.layer.borderWidth=2.0f;
    imgUser.layer.borderColor=bgColor.CGColor;
    //imgUser.image=[UIImage imageNamed:@"welcome_rabecca"];


    NSString *strFullName =[dict objectForKey:@"firstName"];
    lblUserName.text=strFullName;
    lblUserName.textColor=White;
    lblUserName.textAlignment=NSTextAlignmentCenter;
    lblUserName.font=[UIFont fontWithName:@"Kiro" size:14];
    lblUserName.backgroundColor=Black;

    [cell addSubview:lbl];
    [cell addSubview:imgUser];
    [cell addSubview:lblUserName];

}
else if (indexPath.row==1)

{
    UILabel*lbl;
    UILabel*lbln;
    UIImageView*img;
    img=[[UIImageView alloc]initWithFrame:CGRectMake(8, 12, 20, 20)];
    lbl=[[UILabel alloc]initWithFrame:CGRectMake(0, 0, 320, 44)];
    lbln=[[UILabel alloc]initWithFrame:CGRectMake(47, 0, 320, 44)];

    lbl.backgroundColor=Black;
    lbl.layer.borderWidth=1.0f;
    lbl.layer.borderColor=bgColor.CGColor;
    lbln.text=@"Edit Profile";
    lbln.textColor=White;
    lbln.font=[UIFont fontWithName:@"Kiro" size:18];
    img.image=[UIImage imageNamed:@"edit"];

    [cell addSubview:lbl];
    [cell addSubview:lbln];
    [cell addSubview:img];
}
else if (indexPath.row==2)

{
    UILabel*lbl;
    UILabel*lbln;
    UIImageView*img;
    img=[[UIImageView alloc]initWithFrame:CGRectMake(8, 12, 20, 20)];
    lbl=[[UILabel alloc]initWithFrame:CGRectMake(0, 0, 320, 44)];
    lbln=[[UILabel alloc]initWithFrame:CGRectMake(47, 0, 320, 44)];

    lbl.backgroundColor=Black;
    lbl.layer.borderWidth=1.0f;
    lbl.layer.borderColor=bgColor.CGColor;
    lbln.text=@"Match a Friend";
    lbln.textColor=White;
    lbln.font=[UIFont fontWithName:@"Kiro" size:18];
    img.image=[UIImage imageNamed:@"match"];

    [cell addSubview:lbl];
    [cell addSubview:lbln];
    [cell addSubview:img];

}
else if (indexPath.row==3)

{
    UILabel*lbl;
    UILabel*lbln;
    UIImageView*img;
    img=[[UIImageView alloc]initWithFrame:CGRectMake(8, 12, 20, 20)];
    lbl=[[UILabel alloc]initWithFrame:CGRectMake(0, 0, 320, 44)];
    lbln=[[UILabel alloc]initWithFrame:CGRectMake(47, 0, 320, 44)];

    lbl.backgroundColor=Black;
    lbl.layer.borderWidth=1.0f;
    lbl.layer.borderColor=bgColor.CGColor;
    lbln.text=@"Post Charity Events";
    lbln.textColor=White;
    lbln.font=[UIFont fontWithName:@"Kiro" size:18];
    img.image=[UIImage imageNamed:@"addevent"];

    [cell addSubview:lbl];
    [cell addSubview:lbln];
    [cell addSubview:img];

}

else if (indexPath.row==4)
{

    UILabel*lbl;
    UILabel*lbln;
    UIImageView*img;
    img=[[UIImageView alloc]initWithFrame:CGRectMake(8, 12, 20, 20)];
    lbl=[[UILabel alloc]initWithFrame:CGRectMake(0, 0, 320, 44)];
    lbln=[[UILabel alloc]initWithFrame:CGRectMake(47, 0, 320, 44)];

    lbl.backgroundColor=Black;
    lbl.layer.borderWidth=1.0f;
    lbl.layer.borderColor=bgColor.CGColor;
    lbln.text=@"Give to Causes";
    lbln.textColor=White;
    lbln.font=[UIFont fontWithName:@"Kiro" size:18];
    img.image=[UIImage imageNamed:@"give"];

    [cell addSubview:lbl];
    [cell addSubview:lbln];
    [cell addSubview:img];

} else if (indexPath.row==5)
{

    UILabel*lbl;
    UILabel*lbln;
    UIImageView*img;
    img=[[UIImageView alloc]initWithFrame:CGRectMake(8, 12, 20, 20)];
    lbl=[[UILabel alloc]initWithFrame:CGRectMake(0, 0, 320, 44)];
    lbln=[[UILabel alloc]initWithFrame:CGRectMake(47, 0, 320, 44)];

    lbl.backgroundColor=Black;
    lbl.layer.borderWidth=1.0f;
    lbl.layer.borderColor=bgColor.CGColor;
    lbln.text=@"Events";
    lbln.textColor=White;
    lbln.font=[UIFont fontWithName:@"Kiro" size:18];
    img.image=[UIImage imageNamed:@"event"];

    [cell addSubview:lbl];
    [cell addSubview:lbln];
    [cell addSubview:img];


} else if (indexPath.row==6) {

    UILabel*lbl;
    UILabel*lbln;
    UIImageView*img;
    img=[[UIImageView alloc]initWithFrame:CGRectMake(8, 12, 20, 20)];
    lbl=[[UILabel alloc]initWithFrame:CGRectMake(0, 0, 320, 44)];
    lbln=[[UILabel alloc]initWithFrame:CGRectMake(47, 0, 320, 44)];

    lbl.backgroundColor=Black;
    lbl.layer.borderWidth=1.0f;
    lbl.layer.borderColor=bgColor.CGColor;
    lbln.text=@"Market Place";
    lbln.textColor=White;
    lbln.font=[UIFont fontWithName:@"Kiro" size:18];
    img.image=[UIImage imageNamed:@"market"];

    [cell addSubview:lbl];
    [cell addSubview:lbln];
    [cell addSubview:img];


}
else if (indexPath.row==7) {

    UILabel*lbl;
    UILabel*lbln;
    UIImageView*img;
    img=[[UIImageView alloc]initWithFrame:CGRectMake(8, 12, 20, 20)];
    lbl=[[UILabel alloc]initWithFrame:CGRectMake(0, 0, 320, 44)];
    lbln=[[UILabel alloc]initWithFrame:CGRectMake(47, 0, 320, 44)];

    lbl.backgroundColor=Black;
    lbl.layer.borderWidth=1.0f;
    lbl.layer.borderColor=bgColor.CGColor;
    lbln.text=@"Promote Your Business";
    lbln.textColor=White;
    lbln.font=[UIFont fontWithName:@"Kiro" size:18];
    img.image=[UIImage imageNamed:@"market"];

    [cell addSubview:lbl];
    [cell addSubview:lbln];
    [cell addSubview:img];


}
else if (indexPath.row==8) {

    UILabel*lbl;
    UILabel*lbln;
    UIImageView*img;
    img=[[UIImageView alloc]initWithFrame:CGRectMake(8, 12, 20, 20)];
    lbl=[[UILabel alloc]initWithFrame:CGRectMake(0, 0, 320, 44)];
    lbln=[[UILabel alloc]initWithFrame:CGRectMake(47, 0, 320, 44)];

    lbl.backgroundColor=Black;
    lbl.layer.borderWidth=1.0f;
    lbl.layer.borderColor=bgColor.CGColor;
    lbln.text=@"About";
    lbln.textColor=White;
    lbln.font=[UIFont fontWithName:@"Kiro" size:18];
    img.image=[UIImage imageNamed:@"about"];

    [cell addSubview:lbl];
    [cell addSubview:lbln];
    [cell addSubview:img];

}


else if (indexPath.row==9) {

    UILabel*lbl;
    UILabel*lbln;
    UIImageView*img;
    img=[[UIImageView alloc]initWithFrame:CGRectMake(8, 12, 20, 20)];
    lbl=[[UILabel alloc]initWithFrame:CGRectMake(0, 0, 320, 44)];
    lbln=[[UILabel alloc]initWithFrame:CGRectMake(47, 0, 320, 44)];

    lbl.backgroundColor=Black;
    lbl.layer.borderWidth=1.0f;
    lbl.layer.borderColor=bgColor.CGColor;
    lbln.text=@"Terms of Use";
    lbln.textColor=White;
    lbln.font=[UIFont fontWithName:@"Kiro" size:18];
    img.image=[UIImage imageNamed:@"term"];

    [cell addSubview:lbl];
    [cell addSubview:lbln];
    [cell addSubview:img];

}
else if (indexPath.row==10){
    UILabel*lbl;
    UILabel*lbln;
    UIImageView*img;
    img=[[UIImageView alloc]initWithFrame:CGRectMake(8, 12, 20, 20)];
    lbl=[[UILabel alloc]initWithFrame:CGRectMake(0, 0, 320, 44)];
    lbln=[[UILabel alloc]initWithFrame:CGRectMake(47, 0, 320, 44)];

    lbl.backgroundColor=Black;
    lbl.layer.borderWidth=1.0f;
    lbl.layer.borderColor=bgColor.CGColor;
    lbln.text=@"Change Password";
    lbln.textColor=White;
    lbln.font=[UIFont fontWithName:@"Kiro" size:18];
    img.image=[UIImage imageNamed:@"changePassword"];

    [cell addSubview:lbl];
    [cell addSubview:lbln];
    [cell addSubview:img];


} else if (indexPath.row==11){
    UILabel*lbl;
    UILabel*lblLine;
    UILabel*lbln;
    UIImageView*img;
    img=[[UIImageView alloc]initWithFrame:CGRectMake(8, 12, 20, 20)];
    lbl=[[UILabel alloc]initWithFrame:CGRectMake(0, 0, 320, 44)];
    lbln=[[UILabel alloc]initWithFrame:CGRectMake(47, 0, 320, 44)];

    lblLine=[[UILabel alloc]initWithFrame:CGRectMake(0, 39, 320, 1)];
    lblLine.backgroundColor=bgColor;
    lbl.backgroundColor=Black;
    lbl.layer.borderWidth=1.0f;
    lbl.layer.borderColor=bgColor.CGColor;
    lbln.text=@"Logout";
    lbln.textColor=White;
    lbln.font=[UIFont fontWithName:@"Kiro" size:18];
    img.image=[UIImage imageNamed:@"logout"];

    [cell addSubview:lbl];
    [cell addSubview:lbln];
    [cell addSubview:img];
    [cell addSubview:lblLine];
}
/*

 */
// cell.backgroundColor=[UIColor whiteColor];

   return cell;
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.row==0) {
    return 130;
}else if (indexPath.row==13)
{
   return 70;
}else{
    return 40;
}

}

【讨论】:

    【解决方案2】:

    请使用表格视图,优化代码对您来说很容易。您可以在 tableView 的 didSelect 方法中导航每个视图。 这是代码,请尝试一下。

    arrMenuItems = [[NSMutableArray alloc]initWithObjects:@"HOME",@"YOUR WORK", @"BOOKMARKS", @"GETSTARTED",@"SETTING", @"LOG OUT",nil];
    
    
    #pragma mark - Table view delegate and data source
    
    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
        return 1;
    }
    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
        return arrMenuItems.count;
    }
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
        static NSString *cellid = @"cell";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellid];
        if (cell == nil) {
            cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellid];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
        }
    
        cell.textLabel.text = [arrMenuItems objectAtIndex:indexPath.row];
        return cell;
    }
    
    -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
        switch (indexPath.row) {
            case 0:{
    
               HomeViewController *rootViewController = InstantiateVC(@"HomeViewController");
    
                    UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:rootViewController];
                    [navController setViewControllers: @[rootViewController] animated: YES];
    
                    [self.revealViewController setFrontViewController:navController];
                    [self.revealViewController setFrontViewPosition: FrontViewPositionLeft animated: YES];
                }
                break;
            }
    
            case 1:{
                YourWorkViewController *homeVC = InstantiateVC(@"YourWorkViewController");
    
                UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:homeVC];
                [navController setViewControllers: @[homeVC] animated: YES];
    
                [self.revealViewController setFrontViewController:navController];
                [self.revealViewController setFrontViewPosition: FrontViewPositionLeft animated: YES];
    
                break;
            }
    
            case 2:{
                BookMarkViewController *writeToUsVC = InstantiateVC(@"BKBWriteToUsVC");
    
                writeToUsVC.isFromVCTag = WRITE_TO_US;
    
                UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:writeToUsVC];
                [navController setViewControllers: @[writeToUsVC] animated: YES];
    
                [self.revealViewController setFrontViewController:navController];
                [self.revealViewController setFrontViewPosition: FrontViewPositionLeft animated: YES];
                break;
            }
            case 3:{
                GetStartedViewController *aboutUsVC = InstantiateVC(@"BKBAboutVC");
    
    
                UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:aboutUsVC];
                [navController setViewControllers: @[aboutUsVC] animated: YES];
    
                [self.revealViewController setFrontViewController:navController];
                [self.revealViewController setFrontViewPosition: FrontViewPositionLeft animated: YES];
                break;
            }
            case 4:{
                SettingViewController *writeToUsVC = InstantiateVC(@"BKBWriteToUsVC");
    
                writeToUsVC.isFromVCTag = CONTACT_US;
    
                UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:writeToUsVC];
                [navController setViewControllers: @[writeToUsVC] animated: YES];
    
                [self.revealViewController setFrontViewController:navController];
                [self.revealViewController setFrontViewPosition: FrontViewPositionLeft animated: YES];
                break;
            }
            case 5:{
                 LogOutViewController *writeToUsVC = InstantiateVC(@"BKBWriteToUsVC");
    
                writeToUsVC.isFromVCTag = CONTACT_US;
    
                UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:writeToUsVC];
                [navController setViewControllers: @[writeToUsVC] animated: YES];
    
                [self.revealViewController setFrontViewController:navController];
                [self.revealViewController setFrontViewPosition: FrontViewPositionLeft animated: YES];
    
    
    
                break;
            }
    
            default:
                break;
        }
    
    }
    

    请根据您的要求更改您的 ViewController。

    【讨论】:

    • 问题是没有为第一个单元格设置填充,但是上面的代码将如何修复它。不明白请解释一下
    • 但是,非常有趣的是如何在不使用 tableview 重建菜单的情况下为第一个元素设置填充。
    • 这可能是自动布局问题。
    猜你喜欢
    • 2013-11-19
    • 2016-03-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-28
    • 2015-09-07
    • 2019-09-21
    • 2016-01-27
    • 2018-01-04
    相关资源
    最近更新 更多