亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何以編程方式創建基本UIButton?

如何以編程方式創建基本UIButton?

POPMUISE 2019-07-12 16:13:42
如何以編程方式創建基本UIButton?我如何創建一個基本的UIButton以編程的方式?例如,在我的視圖控制器中,當執行viewDidLoad方法三UIButtonS將動態創建,并設置其布局或屬性。
查看完整描述

3 回答

?
米脂

TA貢獻1836條經驗 獲得超3個贊

這里有一個:

UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];[button addTarget:self 
           action:@selector(aMethod:)
 forControlEvents:UIControlEventTouchUpInside];[button setTitle:@"Show View" forState:UIControlStateNormal];
 button.frame = CGRectMake(80.0, 210.0, 160.0, 40.0);[view addSubview:button];


查看完整回答
反對 回復 2019-07-12
?
九州編程

TA貢獻1785條經驗 獲得超4個贊

- (void)viewDidLoad {
    [super viewDidLoad];
    [self addMyButton];   // Call add button method on view load}- (void)addMyButton{    
    // Method for creating button, with background image and other properties

    UIButton *playButton = [[UIButton buttonWithType:UIButtonTypeRoundedRect] retain];
    playButton.frame = CGRectMake(110.0, 360.0, 100.0, 30.0);
    [playButton setTitle:@"Play" forState:UIControlStateNormal];
    playButton.backgroundColor = [UIColor clearColor];
    [playButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal ];
    UIImage *buttonImageNormal = [UIImage imageNamed:@"blueButton.png"];
    UIImage *strechableButtonImageNormal = [buttonImageNormal stretchableImageWithLeftCapWidth:12 topCapHeight:0];
    [playButton setBackgroundImage:strechableButtonImageNormal forState:UIControlStateNormal];
    UIImage *buttonImagePressed = [UIImage imageNamed:@"whiteButton.png"];
    UIImage *strechableButtonImagePressed = [buttonImagePressed stretchableImageWithLeftCapWidth:12 topCapHeight:0];
    [playButton setBackgroundImage:strechableButtonImagePressed forState:UIControlStateHighlighted];
    [playButton addTarget:self action:@selector(playAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:playButton];  }


查看完整回答
反對 回復 2019-07-12
?
瀟瀟雨雨

TA貢獻1833條經驗 獲得超4個贊

目標-C

UIButton *but= [UIButton buttonWithType:UIButtonTypeRoundedRect];[but addTarget:self action:@selector(buttonClicked:)
 forControlEvents:UIControlEventTouchUpInside];
 [but setFrame:CGRectMake(52, 252, 215, 40)];[but setTitle:@"Login" forState:UIControlStateNormal];[but setExclusiveTouch:YES];

 // if you like to add backgroundImage else no need
   [but setbackgroundImage:[UIImage imageNamed:@"XXX.png"] forState:UIControlStateNormal];[self.view addSubview:but];-(void) 
   buttonClicked:(UIButton*)sender {NSLog(@"you clicked on button %@", sender.tag);
 }

斯威夫特

    let myButton = UIButton() // if you want to set the type use like UIButton(type: .RoundedRect) or UIButton(type: .Custom)
    myButton.setTitle("Hai Touch Me", forState: .Normal)
    myButton.setTitleColor(UIColor.blueColor(), forState: .Normal)
    myButton.frame = CGRectMake(15, 50, 300, 500)
    myButton.addTarget(self, action: "pressedAction:", forControlEvents: .TouchUpInside)

    self.view.addSubview( myButton)func pressedAction(sender: UIButton!) {
   // do your stuff here 
  NSLog("you clicked on button %@", sender.tag)}

SWIFT 3及以上

  let myButton = UIButton() // if you want to set the type use like UIButton(type: .RoundedRect) or UIButton(type: .Custom)
    myButton.setTitle("Hi, Click me", for: .normal)
    myButton.setTitleColor(UIColor.blue, for: .normal)
    myButton.frame = CGRect(x: 15, y: 50, width: 300, height: 500)

    myButton.addTarget(self, action: #selector(pressedAction(_:)), for: .touchUpInside)
    self.view.addSubview( myButton)func pressedAction(_ sender: UIButton) {
   // do your stuff here 
  print("you clicked on button \(sender.tag)")}

斯威夫特

例如,從SWIFUIDeveloper門戶

import SwiftUIstruct ContentView : View {
    var body: some View {
        VStack {

            Text("Target Color Black")
             Button(action: { 
                 /* handle button action here */ })
            {
         Text("your Button Name")
          .color(.white)
                        .padding(10)
                        .background(Color.blue)
                        .cornerRadius(5)
                        .shadow(radius: 5)
                        .clipShape(RoundedRectangle(cornerRadius: 5))
     }


        }
    }}#if DEBUGstruct ContentView_Previews : PreviewProvider {
    static var previews: some View {
        ContentView()
    }}#endif


查看完整回答
反對 回復 2019-07-12
  • 3 回答
  • 0 關注
  • 611 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號