Custom UIButton for Iphone

前端 未结 4 741
梦毁少年i
梦毁少年i 2020-12-23 12:49

I have an view in my App which has a number of buttons based on the number of items returned by the server. So if the server returns say 10 items, there should be 10 buttons

4条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-23 13:23

    You first start with a stretchable image with a border:

    alt text http://grab.by/4lP

    Then you make a button with the stretched image as the background and apply text.

    INDEX_OFFSET = 82753; // random
    
    UIButton *sampleButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [sampleButton setFrame:CGRectMake(kLeftMargin, 10, self.view.bounds.size.width - kLeftMargin - kRightMargin, 52)];
    [sampleButton setTitle:@"Button Title" forState:UIControlStateNormal];
    [sampleButton setFont:[UIFont boldSystemFontOfSize:20]];
    [sampleButton setTag:+INDEX_OFFSET];
    [sampleButton setBackgroundImage:[[UIImage imageNamed:@"redButton.png"] stretchableImageWithLeftCapWidth:10.0 topCapHeight:0.0] forState:UIControlStateNormal];
    [sampleButton addTarget:self action:@selector(buttonPressed) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:sampleButton];
    

    Obviously, you will need to adjust the frame origin and size to match your app, as well as the target, selector, and title. And

提交回复
热议问题